<html>
<head>
<title>BetterExplained</title>
<style>
        * {font-family: Consolas; font-size: 16px;}
        .feed {margin: 0.8em 1em 0.8em 1em; border: thin solid gray;}
        .title {padding: 0.3em 0 0.3em 1em; background-color:gray; font-weight:bold; font-size: 20px; text-decoration:none;}
        .content {padding: 0 0 0 1em; }
</style>

</head>
<body>
<div class="feed">
  <div class="title">1. <a href="http://betterexplained.com/articles/a-little-diddy-about-binary-file-formats/" target="_blank">A little diddy about binary file formats</a></div>
  <div class="content"><p>Understanding the nature of file formats and escape characters has been an itch of mine. I recently found a few <a href="http://www.faqs.org/docs/artu/ch05s02.html#id2901882">useful explanations</a> that inspired me to write this. It’s still a work in progress.</p>

<h2>How computers represent data</h2>

<p>Everything is bits and bytes, 1’s and 0’s to the computer. Humans understand text, so we have programs that convert a series of 1’s and 0’s into something we can understand.</p>

<p>In the ASCII character scheme, a single byte (a sequence of 8 1’s or 0’s, or a number from 0-255) can be converted into a character. For example, the character ‘A’ is the number 65 in decimal, 41 in hex, or 01000001. ‘B’ is the number 66 in decimal, and so on (<a href="http://www.lookuptables.com/">full chart</a>).</p>

<p><em>Don’t believe me? Mini-Example time</em></p>

<ul>
<li>Create a file in notepad with the single letter “A” (any filename will do — “sample.txt”).</li>
<li>Save the file, right-click and look the properties — it should be 1 byte (because notepad stores characters in ASCII). The “size on disk” may be larger because the computer allocates space in fixed blocks (of 4 kilobytes, for example).</li>
<li>Find a hex editor (here’s a <a href="http://www.chmaas.handshake.de/delphi/freeware/xvi32/xvi32.htm#download">free one</a>) and open the file you just saved</li>
<li>You’ll see only the single number “41″ in hexadecimal (65 in decimal), and the hex editor may show the character “A” on a side screen (the ASCII representation of the byte you are examining).</li>
<li>This assumes the file is ASCII text (which it is in our case). Try opening a random JPEG to see what’s inside — unprintable numbers will appear as a box.</li>
<li>Try opening a random .exe to see what ASCII strings are embedded inside — you can usually find a few in the beginning portions of the file. Cool, eh?</li>
</ul>

<p>Inside the memory of the computer, only ‘65′ (01000001) is stored, but given the context of the information (i.e., notepad is expecting a text file) the computer knows to display the character ‘a’ on the screen.</p>

<p>Now consider how a human would store the actual <em>numeric value</em> of 65 if you told them to write it down. As humans, we would write it as two characters, a ‘6′ and then a ‘5′, which takes 2 ASCII characters or 2 bytes (again, the “letter” 6 can be stored in ASCII). </p>

<p>A computer would store the number “65″ as 65 in binary, the same as ‘a’. Except this time, software would know that the ‘65′ was not the code for a letter, it was actually the number itself.</p>

<p>Now, suppose we wanted to store the number 4,000,000,000 (4 billion). As humans, we would write it as 4000000000, or 10 ASCII characters (10 bytes). How would a computer do it?</p>

<p>A single byte has 8 bits, or 2^8 (256) possible values. 4 bytes gives us 2^32 bits, or roughly 4 billion values. So, we could store the number 4 billion in only 4 bytes. </p>

<p>As you can see, storing numeric data in the computer’s format saves space. It also saves computational effort — the computer does not have to convert a number between binary and ASCII.</p>

<h2>So, why not use binary formats?</h2>

<p>If binary formats are more efficient, why not use them all the time?</p>

<ul>
<li>Binary files are difficult for humans to read. When a person sees a sequence of 4 bytes, he has no idea what it means (it could be a 4-letter word stored in ASCII). If he sees the 10 ASCII letters 4000000000, he knows it is a number.</li>
<li>Binary files are difficult to edit. In the same manner, if a person wants to change 4 Billion to 2 billion, he needs to know the binary representation. With the ASCII representaion, he can simply put in a “2″ instead of the “4″.</li>
<li>Binary files are difficult to manipulate. The UNIX tradition has several simple, elegant tools to manipulate text. By storing files in the standard text format, you get the power of these tools without having to create special edits to modify your binary file.</li>
<li>Binary files can get confusing. Problems happen when computers have different ways of reading data. There’s something called the “NUXI” problem, which happens when 2 computers with different architectures (Mac and PC, for example) try to transfer binary data (I have an article on this that is being ported). Text data is unambiguous.</li>
<li>The efficiency gain usually isn’t tremendous. Representing numbers in binary can ideally save you a factor of 3 (a 4 byte number can represent 10 bytes of text). However, this assumes that the numbers you are representing are large (a 3-digit number like 999 is better represented in ASCII than as a 4-byte number).  Lastly, ASCII actually only uses 7 bits per byte, so you an theoretically pack ASCII together to get an 1/8 or 12% gain. However, storing text in this way is typically not worth the hassle.</li>
</ul>

<h2>Marshalling and Unmarshalling Data</h2>

<p><em>Aside: Marshalling always makes me thinks of Sheriff Marshals and thus cowboys. Cowboys have nothing to do with the CS meaning of “marshal”.</em></p>

<p>Sometimes computers have complex internaldata structures, with chains of linked items that need to be stored into a file. Marshalling is the process of taking the internal data of a program and saving it to a flat, linear file. Unmarshalling is the process of reading that that linear data and recreating the complex internal data structure the computer originally had.</p>

<p>For example, Microsoft Word must save all document information (page margins, font sizes, embedded macros, styles, etc.) in a linear file. Later, it must be able to read that file and recreate the original setup the user had.</p>

<p>You can marshall data into a binary or text format — the word “marshall” does not indicate how the data is stored.</p>

<h2>So when are binary file formats useful?</h2>

<p>There are situations where you want to use binary file formats. PNG images, for example, where efficiency is important. However, PNG does binary formats right — it specifies byte orders and word lengths to avoid the NUXI problem.</p>

<p>There are often business reasons to use binary formats. The main reasons is that they are more difficult to reverse engineer (humans have to guess how the computer is storing its data), which can help maintain a competitive advantage. </p></div>
</div>

<div class="feed">
  <div class="title">2. <a href="http://betterexplained.com/articles/dns-cache-poisoning/" target="_blank">DNS Cache Poisoning</a></div>
  <div class="content"><p>I found an article explaining <a href="http://www.csgeeks.org/~boyd/Misc/dns-cache-poisoning.html">DNS cache poisoning</a>, an attack used by phishers to redirect “www.mybank.com” to a fake site.</p>

<p>Summary: DNS requests are not authenticated, and therefore the information mapping a domain name to an IP address cannot be trusted. It is possible to send a false broadcast message, such as “The IP of www.banksite.com is A.B.C.D”, to a server.</p>

<p>Many computer protocols (email, IP, DNS, etc.) were designed without authentication in mind– it was a time when security abuses were not a concern. </p></div>
</div>

<div class="feed">
  <div class="title">3. <a href="http://feeds.feedburner.com/~r/Betterexplained/~3/74615348/" target="_blank">Unicode and You</a></div>
  <div class="content"><p>I’m a Unicode newbie. But like many newbies, I had an urge to learn once my interest was piqued by an <a href="http://www.joelonsoftware.com/printerFriendly/articles/Unicode.html" title="introduction to Unicode">introduction to Unicode</a>. </p>

<p>Unicode isn’t hard to understand, but it does cover some low-level CS concepts, like <a href="http://betterexplained.com/articles/understanding-big-and-little-endian-byte-order/" title="Understanding Big and Little Endian Byte Order">byte order</a>. Reading about Unicode is a nice lesson in design tradeoffs and backwards compatibility.</p>

<p>My thoughts are below. Read them alone, or as a follow-up to Joel’s unicode article above. If you’re like me, you’ll get an itch to read about the details in the Unicode specs or in Wikipedia. Really, it can be cool, I swear.</p>

<h2>Key concepts</h2>

<p>Let’s level set on some ideas:</p>

<ul>
<li>Ideas and data are different. The concept of “A” is something different than marks on paper, the sound “aaay” or the number 65 stored inside a computer. </li>
<li>One idea has many possible encodings. An encoding is just a method to transform an idea (like the letter “A”) into raw data (bits and bytes). The idea of “A” can be encoded many different ways. Encodings differ in efficiency and compatibility.</li>
<li>Know thy encoding. When reading data, you must know the encoding used in order to interpret it properly. This is a simple but important concept. If you see the number 65 in binary, what does it really mean? “A” in ASCII? Your age? Your IQ? Unless there is some context, you’d never know. Imagine if someone came up to you and said “65”. You’d have no idea what they were talking about. Now imagine they came up and said “The following number is an ASCII character: 65”. Weird, yes, but see how much clearer it is?</li>
</ul>

<p>Embrace the philosophy that a concept and the data that stores it are different. Let it rustle around in your mind…</p>

<p>Got it? Let’s dive in.</p>

<h2>Back to ASCII and Code Pages</h2>

<p>You’ve probably heard of the ASCII/ANSI characters sets. They map the numeric values 0-127 to various Western characters and control codes (newline, tab, etc.). Note that values 0-127 fit in the lower 7 bits in an 8-bit byte. ASCII does not explicitly define what values 128-255 map to.</p>

<p>Now, ASCII encoding works great for English text (using Western characters), but the world is a big place. What about Arabic, Chinese and Hebrew?</p>

<p>To solve this, computer makers defined “code pages” that used the undefined space from 128-255 in ASCII, mapping it to various characters they needed. Unfortunately, 128 additional characters aren’t enough for the entire world: code pages varied by country (Russian code page, Hebrew code page, etc.).</p>

<p>If people with the same code page exchanged data, all was good. Character #200 on my machine was the same as Character #200 on yours. But if codepages mixed (Russian sender, Hebrew receiver), things got strange.</p>

<p>The character mapped to #200 was different in Russian and Hebrew, and you can imagine the confusion that caused for things like email and birthday invitations. It’s a big IF whether or not someone will read your message using the same codepage you authored your text. If you visit an international website, for example, your browser could try to <em>guess</em> the codepage if it was not specified (”Hrm… this text has a lot of character #213 and #218… probably Hebrew”). But clearly this method was error-prone: codepages needed to be rescued.</p>

<h2>Unicode to the Rescue</h2>

<p>The world had a conundrum: they couldn’t agree on what numbers mapped to what letters in ASCII. The Unicode group went back to the basics: Letters are abstract concepts. Unicode labeled each abstract character with a “code point”. For example, “A” mapped to code point U+0041 (this code point is in hex; code point 65 in decimal). </p>

<p>The Unicode group did the hard work of mapping each character in every language to some code point (not without fierce debate, I am sure). When all was done, the Unicode standard left room for over 1 million code points, enough for all known languages with room to spare for undiscovered civilizations. For fun, you can browse the codepoints with the charmap utility (Start Menu &gt; Run &gt; Charmap) or online at <a href="http://www.unicode.org">Unicode.org</a>.</p>

<p>This brings us to our first design decision: compatibility.</p>

<p>For compatibility with ASCII, code points U+0000 to U+007F (0-127) were the same as ASCII. Purists probably didn’t like this, because the full Latin character sets were defined elsewhere, and now one letter had 2 codepoints. Also, this put Western characters “first”, whereas Chinese, Arabic and the “nonstandard” languages were stuck in the non-sleek codepoints that require 2 bytes to store.</p>

<p>However, this design was necessary – ASCII was a standard, and if Unicode was to be adopted by the Western world it needed to be compatible, without question. Now, the majority of common languages fit into the first 65535 codepoints, which can be stored as 2 bytes.</p>

<p>Phew. The world was a better place, and everyone agreed on what codepoint mapped to what character.</p>

<p>But the question remained: How do we store a codepoint as data?</p>

<h2>Encoding to the Rescue</h2>

<p>From above, encoding turns an idea into raw data. In this case, the idea is a codepoint.</p>

<p>For example, let’s look at the ASCII “encoding” scheme to store Unicode codepoints. The rules are pretty simple:</p>

<ul>
<li>Code points from U+0000 to U+007F are stored in a single byte</li>
<li>Code points above U+0080 are dropped on the floor, never to be seen again</li>
</ul>

<p>Simple, right?</p>

<p>As you can see, ASCII isn’t great for storing Unicode – in fact, it ignores most Unicode codepoints altogether. If you have a Unicode document and save it as ASCII -wham- all your special characters are gone. You’ll often see this as a warning in some text editors when you save Unicode data in a file original saved as ASCII.</p>

<p>But the example has a purpose. An encoding is a system to convert an idea into data. In this case, the conversion can be politely called “lossy”.</p>

<p>I did Unicode experiments with Notepad (can read/write Unicode) and <a href="http://www.pnotepad.org/">Programmer’s Notepad</a>, a hex editor. I wanted to see the raw bytes that notepad was saving. To the examples for yourself:</p>

<ul>
<li>Open notepad and type “Hello”</li>
<li>Save file separately as ANSI, Unicode, Unicode Big Endian, UTF-8</li>
<li>Open file with Programmer’s Notepad and do View &gt; View Hex</li>
</ul>

<h2>All about ASCII</h2>

<p>Let’s write “Hello” in notepad, save as ASNI (ASCII) and open it in a hex editor. It looks like this:</p>

<pre><code>Byte:     48 65 6C 6C 6F
Letter:   H  e  l  l  o
</code></pre>

<p>ASCII is important because many tools and communication protocols only accept ASCII characters. It’s a generally accepted minimum bar for text. Because of its universal acceptance, some Unicode encodings will transform codepoints into series of ASCII characters so they can be transmitted without issue.</p>

<p>Now, in the example above, we know the data is text because we authored it. If we randomly found the file, we could <em>assume</em> it was ASCII text given its contents, but it might be an account number or other data for all we know, that happens to look like “Hello” in ASCII. </p>

<p>Usually, we can make a good guess about what data is supposed to be, based on certain headers or “Magic Numbers” (special character sequences) that appear in certain places. But you can never be sure, and sometimes you can guess wrong. </p>

<p>Don’t believe me? Ok, do the following</p>

<ul>
<li>Open notepad</li>
<li>Write “this program can break”</li>
<li>Save the file as “blah.txt” (or anything else</li>
<li>Open the file</li>
</ul>

<p>Wow… whoa… what happened? I’ll leave this as an exercise for the reader.</p>

<h2>UCS-2 / UTF-16</h2>

<p>This is the encoding I first thought of when I heard “Unicode” – store every character as 2 bytes (what a waste!). At a base level, this can handle codepoints 0×0000 to 0xFFFF, or 0-65535 for you humans out there. And 65,535 should be enough characters for anybody (there are ways to store codepoints above 65535, but read the spec for more details).</p>

<p>Storing data in multiple bytes leads to my favorite conundrum: byte order! Some computers store the little byte first, others the big byte.</p>

<p>To resolve the problem, we can do the following:</p>

<ul>
<li>Option 1: Choose a convention that says all text data must be big or little-endian. This won’t happen – computers on the wrong side of the decision would suffer inefficiency every time they opened a file, since they cannot convert it to the other byte order.</li>
<li>Option 2: Everyone agrees to a byte order mark (BOM), a header at the top of each file. If you open a file and the BOM is backwards, it means it was encoded in a different byte order and needs to be converted.</li>
</ul>

<p>The solution was the BOM header: UCS-2 encodings could write codepoint U+FEFF as a file header. If you open a UCS-2 string and see FEFF, the data is in the right byte order and can be used directly. If you see FFFE, the data came from another type of machine, and needs to be converted to your architecture. This involves swapping every byte in the file.</p>

<p>But unfortunately, things are not that simple. The BOM is actually a valid Unicode character – what if someone sent a file without a header, and that character was actually part of the file?</p>

<p>This is an open issue in Unicode. The suggestion is to avoid U+FEFF except for headers, and use alternative characters instead (there are equivalents). </p>

<p>This opens up design observation #2:
* Multi-byte data will have byte order issues!</p>

<p>ASCII never had to worry about byte order – each character was a single byte, and could not be misinterpreted. But realistically, if you see bytes 0xFEFF or 0xFFEE at the start of a file, it’s a good chance it’s a BOM in a Unicode text file. It’s probably an indication of byte order. Probably.</p>

<h2>UCS-2 Example</h2>

<p>Type “Hello” in notepad and save it as Unicode (little-endian UCS-2 is the native format on Windows): </p>

<p>Hello-little-endian:</p>

<pre><code>FF FE  4800 6500 6C00 6c00 6F00
header H    e    l    l    o
</code></pre>

<p>Save it again as Unicode Big Endian, and you get:</p>

<p>Hello-big-endian:</p>

<pre><code>FE FF  0048 0065 006C 006C 006F
header H    e    l    l    o
</code></pre>

<p>Observations</p>

<ul>
<li>The header BOM (U+FEFF) shows up as expected: FF FE for little-endian, FEFF for big</li>
<li>Letters use 2 bytes no matter what: “H” is 0×48 in ASCII, and 0×0048 in UCS-2</li>
<li>Encoding is simple. Take the codepoint in hex and write it out in 2 bytes. No extra processing is required.</li>
<li>The encoding is too simple. It wastes space for plain ASCII text that does not use the high-order byte. And ASCII text is very common.</li>
<li>The encoding inserts null bytes (0×00) which can be a problem. Old-school ASCII programs may think the Unicode string has ended when it gets to the null byte. On a little-endian machine, reading one byte at a time, you’d get to H (H = 0×4800) and then hit the null and stop. On a big endian machine, you’d hit the null first (H is 0×0048) and not even see the H in ASCII. Not good.</li>
</ul>

<p>Design observation #3
* Consider backwards compatibility. How will an old program read new data? Ignoring new data is good. Breaking on new data is bad.</p>

<h2>UTF-8</h2>

<p>UCS-2 / UTF-16 is nice and simple, but boy it does waste some bits. Not only does it double ASCII, but the converted ASCII might not even be readable due to the null characters.</p>

<p>Enter UTF-8. Its goal is to encode Unicode characters in single byte where possible (ASCII), and not break ASCII applications by having null characters. It is the default encoding for XML.</p>

<p>Read the UTF-8 specs for more detail, but at a high level:</p>

<ul>
<li>Code points 0 - 007F are stored as regular, single-byte ASCII.</li>
<li>Code points 0080 and above are converted to binary and stored (encoded) in a series of bytes.</li>
<li><p>The first “count” byte indicates the number of bytes for the codepoint, including the count byte. These bytes start with 11..0:</p>

<p>110xxxxx (11 -&gt; 2 bytes in sequence, including “count” byte)
1110xxxx (1110 -&gt; 3 bytes in sequence)
11110xxx (11110 -&gt; 4 bytes in sequence)</p></li>
<li><p>Bytes starting with 10… are “data” bytes and contain information for the codepoint. A 2-byte example looks like this</p>

<p>110xxxxx 10xxxxxx </p></li>
</ul>

<p>This means there are 2 bytes in the sequence. The X’s represent the binary value of the codepoint, which needs to squeeze in the remaining bits.</p>

<p>Observations about UTF-8</p>

<ul>
<li>No null bytes. All ASCII characters (0-127) are the same. Non-ASCII characters all start with “1” as the highest bit.</li>
<li>ASCII text is stored identically and efficiently.</li>
<li>Unicode characters start with “1” as the high bit, and can be ignored by ASCII-only programs (however, they may be discarded in some cases! See UTF-7 for more details).</li>
<li>There is a time-space tradeoff. There is processing to be done on every Unicode character, but this is a reasonable tradeoff.</li>
</ul>

<p>Design principle #4</p>

<ul>
<li>UTF-8 addresses the 80% case well (ASCII), while making the other cases possible (Unicode). UCS-2 addresses all cases equally, but is inefficient in the 80% case for solve for the 99% case. But UCS-2 is less processing-intensive than UTF-8, which requires bit manipulation on all Unicode characters.</li>
<li>Why does XML store data in UCS-8 instead of UCS-2? Is space or processing power more important when reading XML documents?</li>
<li>Why does Windows XP store strings as UCS-2 natively? Is space or processing power more important for the OS internals?</li>
</ul>

<p>In any case, UTF-8 still needs a header to indicate how the text was encoded. Otherwise, it could be interpreted as straight ASCII with some codepage to handle values above 127. It still uses the U+FEFF codepoint as a BOM, but the BOM itself is encoded in UTF-8 (clever, eh?).</p>

<h2>UTF-8 Example</h2>

<p>Hello-UTF-8:</p>

<pre><code>EF BB BF 48 65 6C 6C 6F
header   H  e  l  l  o
</code></pre>

<p>Again, the ASCII text is not changed in UTF-8. Feel free to use charmap to copy in some Unicode characters and see how they are stored in UTF-8. Or, you can <a href="http://www.stanford.edu/~hc10/misc/binhexuni.html">experiment online</a>.</p>

<h2>UTF-7</h2>

<p>While UTF-8 is great for ASCII, it still stores Unicode data as non-ASCII characters with the high-bit set. Some email protocols do not allow non-ASCII values, so UTF-8 data would not be sent properly.</p>

<p>Enter UTF-7. The goal is to encode Unicode data in 7 bits (0-127), which is compatible with ASCII. Storing data without using the top bit is known as “8-bit clean”.</p>

<p>UTF-7 works like this</p>

<ul>
<li>Codepoints in the ASCII range are stored as ASCII, except for certain symbols (+, -) that have special meaning</li>
<li>Codepoints above ASCII are converted to binary, and stored in base64 encoding (stores binary information in ASCII)</li>
</ul>

<p>How do you know which ASCII letters are real ASCII, and which are base64 encoded? Easy. ASCII characters between the special symbols “+” and “-“ are considered base64 encoded. </p>

<p>“-” acts like an escape suffix character. If it follows a character, that item is interpreted literally. So, “+-“ is interpreted as “+” without any special encoding. This is how you store an actual “+” symbol in UTF-7.</p>

<h2>UTF-7 Example</h2>

<p>Wikipedia has some UTF-7 examples, Notepad can’t save as UTF-7.</p>

<p>“Hello, World!” is unchanged (all ASCII characters, no fancy symbols)
“£1″ (1 British pound) becomes “+AKM-1″. The characters “+AKM-” means AKM should be decoded in base64 and converted to a codepoint, which maps to 0×00A3 or the British pound symbol. The “1” is kept the same, since it is a ASCII character.</p>

<p>UTF is pretty clever, eh?</p>

<h2>Wrapping it up – what I’ve learned</h2>

<p>I’m still a newbie but have learned a few things about Unicode:</p>

<ul>
<li>Unicode does not mean 2 bytes. Unicode defines code points that can be stored in many different ways (UCS-2, UTF-8, UTF-7, etc.).  Encodings vary in simplicity and efficiency.</li>
<li>Unicode has more than 65,535 (16 bits) worth of characters. Encodings can specify more characters, but the first 65535 cover most of the common languages. </li>
<li>You need to know the encoding to correctly read a file. You can often <em>guess</em> that a file is Unicode based on the Byte Order Mark (BOM), but confusion can still arise unless you know the exact encoding. Even text that looks like ASCII could actually be encoded with UTF-7; you just don’t know.</li>
</ul>

<p>Unicode is an interesting study. It opened my eyes to design tradeoffs, and the importance of separating the core idea from the encoding used to save it. </p></div>
</div>

<div class="feed">
  <div class="title">4. <a href="http://feeds.feedburner.com/~r/Betterexplained/~3/74615347/" target="_blank">Sorting Algorithms</a></div>
  <div class="content"><p>(Still a work-in progress; I want to revisit with intuitive explanations and playing-card examples)</p>

<p>Sorting is a key to CS theory, but easy to forget. I had an itch to review the algorithms in Wikipedia (strange, I know), and here are my notes: </p>

<h2>High-level thoughts</h2>

<ul>
<li>Some algorithms (selection, bubble, heapsort) work by moving elements to their final position, one at a time. You sort an array of size N, put 1 item in place, and continue sorting an array of size N - 1 (heapsort is slightly different).</li>
<li>Some algorithms (insertion, quicksort, counting, radix) put items into a temporary position, close(r) to their final position. You rescan, moving items closer to the final position with each iteration.</li>
<li>One technique is to start with a “sorted list” of one element, and merge unsorted items into it, one at a time.</li>
<li>Complexity and running time
<ul>
<li>Factors: algorithmic complexity, startup costs, additional space requirements, use of recursion (funtion calls are expensive and eat stack space), worst-case behavior, assumptions about input data, caching, and behavior on already-sorted or nearly-sorted data</li>
<li>Worst-case behavior is important for real-time systems that need guaranteed performance. For security, you want the guratantee that data from an attacker does not have the ability to overwhelm your machine.</li>
<li>Caching — algorithms with sequential comparisons take advantage of spatial locality and prefetching, which is good for caching.</li>
<li>Algorithmic time vs. real time — The simple algorithms may be O(N^2), but have low overhead. They can be faster for sorting small data sets (&lt; 10 items). One compromise is to use a different sorting method depending on the input size. </li>
<li>“Comparison sorts” make no assumptions on the data and compare all elements against each other (majority of sorts). O(N lg N) time is the ideal “worst-case” scenario (if that makes sense — O(N lg N) is the smallest penalty you can hope for in the worst case). Heapsort has this behavior.</li>
<li>O(N) time is possible if we make assumptions about the data and don’t need to compare elements against each other (i.e., we know the data falls into a certain range or has some distribution). O(N) clearly is the minimum sorting time possible, since we must examine every element at least once (how can you sort an item you do not even examine?).</li>
</ul></li>
</ul>

<h2>Notes</h2>

<ul>
<li>Assume we are sorting a list or array of N elements</li>
<li>Once sorted, smaller items are on the left (first item) and larger items are on the right (last item)</li>
</ul>

<h2>Bubble Sort [Best: O(n), Worst:O(N^2)]</h2>

<p>Starting on the left, compare adjacent items and keep “bubbling” the larger one to the right (it’s in its final place). Bubble sort the remaining N -1 items.</p>

<ul>
<li>Though “simple” I found bubble sort nontrivial. In general, sorts where you iterate backwards (decreasing some index) were counter-intuitive for me. With bubble-sort, either you bubble items “forward” (left-to-right) and move the endpoint backwards (decreasing), or bubble items “backward” (right-to-left) and increase the left endpoint. Either way, some index is decreasing. </li>
<li>You also need to keep track of the next-to-last endpoint, so you don’t swap with a non-existant item.</li>
</ul>

<h2>Selection Sort [Best/Worst: O(N^2)]</h2>

<p>Scan all items and find the smallest. Swap it into position as the first item. Repeat the selection sort on the remaining N-1 items.</p>

<ul>
<li>I found this the most intuitive and easiest to implement — you always iterate forward (i from 0 to N-1), and swap with the smallest element (always i).</li>
</ul>

<h2>Insertion Sort [Best: O(N), Worst:O(N^2)]</h2>

<p>Start with a sorted list of 1 element on the left, and N-1 unsorted items on the right. Take the first unsorted item (element #2) and insert it into the sorted list, moving elements as necessary. We now have a sorted list of size 2, and N -2 unsorted elements. Repeat for all elements.</p>

<ul>
<li>Like bubble sort, I found this counter-intuitive because you step “backwards”</li>
<li>This is a little like bubble sort for moving items, except when you encounter an item smaller than you, you stop. If the data is reverse-sorted, each item must travel to the head of the list, and this becomes bubble-sort. </li>
<li>There are various ways to move the item leftwards — you can do a swap on each iteration, or copy each item over its neighbor</li>
</ul>

<h2>Quicksort [Best: O(N lg N), Avg: O(N lg N), Worst:O(N^2)]</h2>

<p>There are may versions of Quicksort, which is one of the most popular sorting methods due to its speed (O(N lgN) average, but O(N^2) worst case). Here’s a few:</p>

<p>Using external memory:</p>

<ul>
<li>Pick a “pivot” item</li>
<li>Partition the other items by adding them to a “less than pivot” sublist, or “greater than pivot” sublist</li>
<li>The pivot goes between the two lists </li>
<li>Repeat the quicksort on the sublists, until you get to a sublist of size 1 (which is sorted). </li>
<li>Combine the lists — the entire list will be sorted</li>
</ul>

<p>Using in-place memory:</p>

<ul>
<li>Pick a pivot item and swap it with the last item. We want to partition the data as above, and need to get the pivot out of the way. </li>
<li>Scan the items from left-to-right, and swap items greater than the pivot with the last item (and decrement the “last” counter). This puts the “heavy” items at the end of the list, a little like bubble sort.</li>
<li>Even if the item previously at the end is greater than the pivot, it will get swapped again on the next iteration.</li>
<li>Continue scanning the items until the “last item” counter overlaps the item you are examining - it means everything past the “last item” counter is greater than the pivot.</li>
<li>Finally, switch the pivot into its proper place. We know the “last item” counter has an item greater than the pivot, so we swap the pivot there. </li>
<li>Phew! Now, run quicksort again on the left and right subset lists. We know the pivot is in its final place (all items to left are smaller; all items to right are larger) so we can ignore it. </li>
</ul>

<p>Using in-place memory w/two pointers:</p>

<ul>
<li>Pick a pivot and swap it out of the way</li>
<li>Going left-to-right, find an oddball item that is greater than the pivot</li>
<li>Going right-to-left, find an oddball item that is less than the pivot</li>
<li>Swap the items if found, and keep going until the pointers cross — re-insert the pivot</li>
<li>Quicksort the left and right partitions</li>
<li>Note: this algorithm gets confusing when you have to keep track of the pointers and where to swap in the pivot</li>
</ul>

<p>Notes</p>

<ul>
<li>If a bad pivot is chosen, you can imagine that the “less” subset is always empty. That means we are only creating a subset of one item smaller each time, which gives us O(N^2) behavior in the worst case.</li>
<li>If you choose the first item, it may be the smallest item in a sorted list and give worst-case behavior. You can choose a random item, or median-of-three (front, middle, end).</li>
<li>Quicksort is <em>fast</em> because it uses spatial locality — it walks neighboring elements, comparing them to the pivot value (which can be stored in a register). It makes very effective use of caching.</li>
<li>The pivot is often swapped to the front, so it is out of the way during the pivotting. Afterwards, it is swapped into place (with a pivot item that is less than or equal to it, so the pivot is preservd).</li>
<li>The quicksort algorithm is complicated, and you have to pass left and right boundary variables</li>
</ul>

<h2>Heapsort [Best/Avg/Worst: O(N lg N)]</h2>

<p>Add all items into a heap. Pop the largest item from the heap and insert it at the end (final position). Repeat for all items.</p>

<ul>
<li>Heapsort is just like selection sort, but with a better way to get the largest element. Instead of scanning all the items to find the max, it pulls it from a heap. Heaps have properties that allow heapsort to work in-place, without additional memory.</li>
<li>Creating the heap is O(N lg N). Popping items is O(1), and fixing the heap after the pop is lgN. There are N pops, so there is another O(N lgN) factor, which is O(N lg N) overall.</li>
<li>Heapsort has O(N lgN) behavior, even in the worst case, making it good for real-time applications</li>
</ul>

<h2>Counting sort [Best/Avg/Worst: O(N)]</h2>

<p>Assuming the data are integers, in a range of 0-k. Create an array of size K to keep track of how many items appear (3 items with value 0, 4 items with value 1, etc). Given this count, you can tell the position of an item — all the 1’s must come after the 0’s, of which there are 3. Therefore, the 1’s start at item #4. Thus, we can scan the items and insert them into their proper position.</p>

<ul>
<li>Creating the count array is O(N)</li>
<li>Inserting items into their proper position is O(N)</li>
<li>I oversimplified here — there is a summation of the counts, and a greatest-to-least ordering which keeps the sort stable.</li>
</ul>

<h2>Radix sort [Best/Avg/Worst: O(N)]</h2>

<p>Get a series of numbers, and sort them one digit at a time (moving all the 1000’s ahead of the 2000’s, etc.). Repeat the sorting on each set of digits.</p>

<ul>
<li>Radix sort uses counting sort for efficient O(N) sorting of the digits (k = 0…9)</li>
<li>Actually, radix sort goes from least significant digit (1’s digit) to most significant, for reasons I’ll explain later (see CLRS book)</li>
<li>Radix &amp; counting sort are fast, but require structured data, external memory and do not have the caching benefits of quicksort.</li>
</ul>

<h2>Actually doing the sorts</h2>

<p>For practice, I wrote most of the sorts above in C, based on the psuedocode. Findings</p>

<ul>
<li>Even “easy” sorts like bubble sort get complicated with decrements, off-by-one errors, &gt; vs &gt;= as you try to avoid walking off the end of the array with a swap.</li>
<li>Mocking up the problem on paper is crucial, just like writing the code to swap items in a linked list. Don’t have it all in your head.</li>
<li>I found and fixed bugs in all of my initial sorts. Create a good test harness that makes it <em>easy</em> to test.
<ul>
<li>I separated my sorting routines into a DLL (I’m learning how to do Windows programming — it’s pretty different from Unix)</li>
<li>I created a simple command-line .exe that took a list of numbers, turned them into an array, and called my sorting function, printing the result. This type of testing was encouraged by Kernighan — the tests are easy, do not require compilation (such as hard-coding a “testing” program)</li>
</ul></li>
<li>Because testing was easy, I made every test case I could think of: Pre-sorted forward, backwards, 1 element, 2 elements, even and odd items, etc.</li>
<li>For debugging, I printed the intermediate array at each stage of the sort. </li>
</ul>

<h2>References</h2>

<ul>
<li><a href="http://en.wikipedia.org/wiki/Sorting_algorithms">Wikipedia Sorting Algorithms</a></li>
<li><a href="http://www.amazon.com/exec/obidos/ASIN/0262032937/instacalconli-20"><img src="http://images.amazon.com/images/P/0262032937.01.THUMBZZZ.jpg" alt="Book Cover">Introduction to Algorithms by CLRS</a> </li>
</ul></div>
</div>

<div class="feed">
  <div class="title">5. <a href="http://feeds.feedburner.com/~r/Betterexplained/~3/74615346/" target="_blank">Programming Gotchas</a></div>
  <div class="content"><p> What are gotchas? They’re itty-bitty details that will bite you because you didn’t say the secret word or rub your belly 3 times while pressing the Enter key. Gotcha!</p>

<h2>Internet Explorer</h2>


<ul>
<li>You can’t have a popup window with a <a href="http://www.firsttube.com/read/Internet-Explorer-Sucks" title="space in the name">space in the name</a>.</li>
<li>When making an <span>HTA, </span>don’t open links inside the <span>HTA.</span> You can’t use <code>javascript:void(0)</code> links, as it tries to open it as a new page and you get pleasant security warnings. Use “#” (which, unfortunately, can cause you to jump around the screen. Then use #top…)</li>
</ul>



<h2>Firefox</h2>


<ul>
<li>There’s a <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=236791" title="focus bug in input areas">focus bug in input areas</a>. Workaround: Need an autocomplete flag</li>
</ul>





<pre>
&lt;input type=&quot;text&quot; autocomplete=&quot;OFF&quot;&gt;
</pre>



<h2>Programming</h2>


<ul>
<li>Always, always rebuild clean. Check this first - you could be using old files, be in the wrong directory, or in the wrong terminal window.</li>
</ul>



<h2>Javascript</h2>


<ul>
<li><code>object[name] != object['name']</code>. Beware a string in variable name and the string “name”. Confusing? Yes. Maybe there’s something to hungarian notation after all (no, no, just kidding!)</li>
</ul>




<ul>
<li><span>CSS</span>: <code>font-size: 10px</code> becomes variable <code>object.style.fontSize</code> in javascript. <span>CSS </span>proprties without dashes are the same (i.e., height).</li>
</ul>




<ul>
<li><code>onpaste/oninput</code> detects paste events inside an input area from the mouse (<code>onkeypress</code> won’t work).</li>
</ul>




<h2>Apache</h2>


<ul>
<li>Need the qsa flag in order to pass the query string through for mod-rewrite rules (otherwise, you just get the page and not the query). </li>
</ul></div>
</div>

<div class="feed">
  <div class="title">6. <a href="http://feeds.feedburner.com/~r/Betterexplained/~3/74615345/" target="_blank">Getting Going with Wordpress</a></div>
  <div class="content"><p> A list of what I’ve done to get the Wordpress blog software up and running.</p>

<h2>Get setup</h2>


<ul>
<li><a href="http://wordpress.org" title="Download">Download</a>, extract</li>
<li>Create database in <span>PHPM</span>yAdmin</li>
<li>Run setup</li>
</ul>



<h2>Get a theme you like</h2>


<ul>
<li><a href="http://www.urbangiraffe.com/themes/" title="UrbanGirrafe">UrbanGirrafe</a></li>
<li><a href="http://www.plaintxt.org/2006/06/13/the-best-minimalist-wordpress-themes/" title="Minimalist themes">Minimalist themes</a></li>
</ul>




<h2>Tweak</h2>


<ul>
<li>Update your theme colors, text, layout</li>
<li>Better title tags using the Optimal Title plugin. I’m not a huge fan of <span>SEO, </span>but it helps to have the title come after the post name.</li>
</ul>




<ul>
<li>Better permalinks: <code>/articles/%postname%/</code></li>
</ul>




<ul>
<li>Better Archives: Just show me a list of all my posts, please!</li>
</ul>





<pre><code>&lt;h2&gt;All Posts&lt;/h2&gt;
&lt;ul class=&quot;archive-items&quot;&gt;
&lt;?php&gt;
$posts =
get_posts(&#39;order=DESC&amp;numberposts=50000&#39;);
foreach ($posts as $post) :
?&gt;
&lt;li&gt;&lt;span class=&quot;date&quot;&gt;&lt;?php the_time(&#39;M d Y&#39;) ?&gt;&lt;/span&gt;
&lt;a href=&quot;&lt;?php the_permalink() ?&gt;&quot;&gt;&lt;?php the_title(); ?&gt;&lt;/a&gt;
&lt;span class=&quot;date&quot;&gt; in&lt;/span&gt;
&lt;?php the_category(&#39;, &#39;) ?&gt;&lt;/li&gt;
&lt;?php endforeach; ?&gt;
&lt;/ul&gt;</code></pre>



<h2>Read Blogs &amp; Articles</h2>


<ul>
<li>Wordpress blogging at <a href="http://blog.taragana.com/index.php/wordpress-only/" title="taragana">taragana</a></li>
<li><a href="http://www.headzoo.com/wptcs.png" title="Template cheatsheet">Template cheatsheet</a></li>
<li><a href="http://lorelle.wordpress.com/" title="Lorelle on wordpress">Lorelle on wordpress</a></li>
<li><a href="http://internet.newsforge.com/article.pl?sid=06/01/30/1724203&amp;tid=13" title="Other plugins">Other plugins</a></li>
<li><a href="http://www.chris.narrabilis.com/wp/2006/02/28/an-adsense-for-wordpress-tutorial/" title="Adsense">Adsense</a></li>
<li><a href="http://www.tamba2.org.uk/wordpress/" title="Tamba2, Excellent Wordpress guides">Tamba2, Excellent Wordpress guides</a></li>
<li><a href="http://www.bloggingpro.com/archives/category/wordpress-tips/" title="Use technorati tags">Use technorati tags</a></li>
<li><a href="http://www.problogger.net/archives/2005/12/30/three-simple-actions-that-doubled-my-website-traffic-in-30-days/" title="Problogger blogging tips">Problogger blogging tips</a></li>
<li><a href="http://weblogtoolscollection.com/archives/2004/07/05/wordpress-12-wiki-integration/" title="Wiki plugin">Wiki plugin</a></li>
<li><a href="http://theundersigned.net/2006/06/wordpress-and-seo" title="SEO">SEO</a></li>
</ul>



<h2>Plugins I actually use</h2>


<ul>
<li><a href="http://mnm.uib.es/gallir/wp-cache-2/" title="WP-Cache">WP-Cache</a> - very fast caching plugin. Remember to <code>chmod a+x</code> the .php files after uploading.</li>
</ul>





<pre>
<code>Add to wp-config.php: define(&#39;WP_CACHE&#39;, true);
Added to cached pages:
&lt;!-- Dynamic Page Served (once) in 0.601 seconds --&gt;
&lt;!-- Cached page served by WP-Cache --&gt;</code>
</pre>




<ul>
<li><a href="http://www.neato.co.nz/ultimate-tag-warrior/" title="Ultimate Tag Warrior">Ultimate Tag Warrior</a> - Easy tagging, integrates with…</li>
</ul>




<ul>
<li><a href="http://www.headzoo.com/" title="Edit In Place">Edit In Place</a> Killer plugin, lets you edit posts “in place” rather than opening up wordpress. Makes your blog more wiki-like in editing.</li>
</ul>




<ul>
<li><a href="http://www.semiologic.com/software/smart-link/" title="Smart Link">Smart Link</a> Create links to other posts using</li>
</ul>




<pre>
[ my text -&gt; my link ]
</pre>




<ul>
<li>“my link” can be a full <span>URL, </span>relative path, category, post slug. Easiest way to cross-link posts.</li>
</ul>




<ul>
<li><a href="http://dev.wp-plugins.org/wiki/TextControl" title="TextControl">TextControl</a> - I couldn’t decide between Markdown and Textile and this lets me choose on a per-post basis (or no special formatting).</li>
</ul>




<ul>
<li><a href="http://orderedlist.com/wordpress-plugins/feedburner-plugin/" title="Feedburner Feed replacement">Feedburner Feed replacement</a> Feedburner lets you track your <span>RSS </span>readers. This redirects your feed to feedburner, and is the most flexible option (you can disable the plugin and serve feeds from your normal site - users always have the same <span>URL</span>).</li>
</ul>




<ul>
<li><a href="http://akismet.com/" title="Akismet anti-spam">Akismet anti-spam</a> Comment spam is bad. Nuff said.</li>
</ul>




<ul>
<li><a href="http://sixthform.info/steve/wordpress/index.php" title="LatexRenderer">LatexRenderer</a> I’m a geek and want to write formulas in my posts using Latex. Install this if you want to as well.</li>
</ul>




<ul>
<li><a href="http://www.thunderguy.com/semicolon/wordpress/code-markup-wordpress-plugin/" title="Code Markup">Code Markup</a> Again, for geeks that want to easily include code in their posts without having to escape every &gt; and &lt;.</li>
</ul>




<ul>
<li><a href="http://soderlind.no/ImageManager" title="ImageManager">ImageManager</a> Best plugin I’ve tried to handle images in posts.</li>
</ul>




<ul>
<li><a href="http://cavemonkey50.com/code/google-analyticator" title="Google Analyticator">Google Analyticator</a> Easily add your google analytics code to your blog.</li>
</ul>




<ul>
<li><a href="http://ryanduff.net/projects/wp-contactform/" title="WP-ContactForm">WP-ContactForm</a> Add an email form to your site. Great for getting quick feedback - not everyone wants to email you at your real address.</li>
</ul>




<ul>
<li><a href="http://www.scotthough.com/wordpress-translate/" title="WP-Translate">WP-Translate</a> Adds links to Google Translate to make your blog available in other languages. Use <code>&lt;?php translate(n); ?&gt;</code> to add the sidebar.</li>
</ul>




<ul>
<li><a href="http://elasticdog.com/2004/09/optimal-title/" title="Optimal Title">Optimal Title</a> - Make the title of your blog come after your post name (better for search engines and looks better too).</li>
</ul>




<pre>
<code>
&lt;title&gt;&lt;?php if ( function_exists(&#39;optimal_title&#39;) )
{ optimal_title(&#39;|&#39;); bloginfo(&#39;name&#39;); } 
else { bloginfo(&#39;name&#39;); wp_title(&#39;|&#39;); } ?&gt;
&lt;?php if ( is_home() ) { ?&gt; | 
&lt;?php bloginfo(&#39;description&#39;); } ?&gt;&lt;/title&gt;
</code>
</pre>




<ul>
<li><a href="http://www.arnebrachhold.de/2005/06/05/google-sitemaps-generator-v2-final" title="Google Sitemap Plugin">Google Sitemap Plugin</a> Sitemaps are a list of all the pages on your site, which help Google and other search engines crawl your site. Use them.</li>
</ul>




<ul>
<li><a href="http://erwin.terong.com/2005/09/24/wp-plugin-related-posts-link/" title="Related Posts">Related Posts</a> List articles related to the current one, good for helping your readers explore the site. Make sure to wrap the function call:</li>
</ul>




<pre>
<code>&lt;?php if (function_exists(&#39;related_posts&#39;)) related_posts(); ?&gt;
</code>
</pre>

<p> </p></div>
</div>

<div class="feed">
  <div class="title">7. <a href="http://feeds.feedburner.com/~r/Betterexplained/~3/74615344/" target="_blank">Law of Unintended Consequences</a></div>
  <div class="content"><p> Every action has a predicted consequence — along with side-effects the actor did not dream of (<a href="http://en.wikipedia.org/wiki/Law_of_unintended_consequences" title="see wikipedia">see wikipedia</a>). A few examples:</p>


<ul>
<li>Railroads forced the creation of a national time-table. Previously, cities kept their own time. New York and Philadelphia could be 5 minutes off, for example, which would disrupt train schedules.</li>
</ul>




<ul>
<li>The car gave rise to the current popularity of radio. If not for the car, when else would you be stuck in an enclosed area with nothing to do? At home, the TV clearly dominates.</li>
</ul>




<ul>
<li><a href="http://instacalc.com" title="InstaCalc">InstaCalc</a>, a project I’m developing, was developed for an English-speaking audience. However, the calculator interface is inherently multi-lingual (numbers are numbers wherever you go) and much to my surprise, a huge portion of my traffic comes from non-English speaking countries.</li>
</ul>



<p>Most inventions will have a side-effect you didn’t think of, even taking this law into account <img src="http://betterexplained.com/wp-includes/images/smilies/icon_smile.gif" alt=":)"> . Keep your mind and design open to change. </p></div>
</div>

<div class="feed">
  <div class="title">8. <a href="http://feeds.feedburner.com/~r/Betterexplained/~3/74615343/" target="_blank">Number Systems and Bases</a></div>
  <div class="content"><p> Base systems like binary and hexadecimal seem a bit strange at first. The key is understanding how different systems “tick over” like an odometer when they are full. Base 10, our decimal system, “ticks over” when it gets 10 items, creating a new digit. We wait 60 seconds before “ticking over” to a new minute. Hex and binary are similar, but tick over every 16 and 2 items, respectively.</p>

<p>Try converting numbers to hex and binary here:</p>

<p></p>

<h2>Way back when: Unary Numbers</h2>

<p>Way back in the day, we didn’t have base systems! It was uphill both ways, through the snow and blazing heat. When you wanted to count one, you’d write:</p>

<p>l</p>

<p>When you wanted 5, you’d write</p>

<p>lllll</p>

<p>And clearly, 1 + 5 = 6</p>

<p>l + lllll = llllll</p>

<p>This is the simplest way of counting.</p>

<h2>Enter the Romans</h2>

<p>In Roman numerals, two was one, twice. Three was one, thrice:</p>

<p>one = I<br>
two = II<br>
three = <span>III</span></p>

<p>However, they decided they could do better than the old tradition of lines in the sand. For five, we could use V to represent lllll and get something like</p>

<p>l + V = Vl</p>

<p>Not bad, eh? And of course, there are many more symbols (L, C, M, etc.) you can use.</p>

<p>The key point is that V and lllll are two ways of encoding the number 5.</p>

<h2>Give each number a name</h2>

<p>Another breakthrough was realizing that <strong>each number</strong> can be its own distinct concept. Rather than represent three as a series of ones, give it its own symbol: “3″. Do this from one to nine, and you get the symbols:</p>

<p>1 2 3 4 5 6 7 8 9</p>

<p>The Romans were close, so close, but only gave unique symbols to 5, 10, 50, 100, 1000, etc.</p>

<h2>Use your position</h2>

<p>Now clearly, you can’t give <strong>every</strong> number its own symbol. There’s simply too many <img src="http://betterexplained.com/wp-includes/images/smilies/icon_smile.gif" alt=":)"> </p>

<p>But notice one insight about Roman numerals: they use <strong>position</strong> of symbols to indicate meaning.</p>

<p>IV means “subtract 1 from 5″</p>

<p>and VI means “add 1 to 5″.</p>

<p>In our number system, we use position in a similar way. We <strong>always add</strong> and never subtract. And each position is 10 more than the one before it. </p>

<p>So, 35 means “add 3*10 to 5*1″ and 456 means “4*100 + 5*10 + 6*1″.</p>

<h2>Our choice of base 10</h2>

<p>Why did we choose to multiply by 10 each time? Most likely because we have 10 fingers.</p>

<p>One point to realize is you need enough digits to “fill up” until you hit the next number. Let me demonstrate.</p>

<p>If we want to roll the odometer over every 10, so to speak, we need symbols for numbers one through nine; we haven’t reached ten yet. Imagine numbers as ticking slowly upward - at what point do you flip over the next unit and start from nothing?</p>

<h2>Enter zero</h2>

<p>And what happens when we reach ten? How do we show we want exactly one “ten” and nothing in the “ones” column?</p>

<p>We use zero, the number that doesn’t exist. Zero is quite a concept, it’s a placeholder, a blank, a space, and a whole lot more. Suffice it to say, Zero is one of the great inventions of all time.</p>

<p>Zero allows us to have an empty placeholder, something the Romans didn’t have. Look how unwieldly their numbers are without it.</p>

<p>George Orwell’s famous novel “1984″ would be “MCMLXXXIV”! Rolls right off the tongue, doesn’t it? <img src="http://betterexplained.com/wp-includes/images/smilies/icon_smile.gif" alt=":)"> </p>

<h2>Considering other bases</h2>

<p>Remember that we <strong>chose</strong> to roll over our odometer every ten. Our counting looks like this:</p>

<p> 1<br>
 2<br>
 3<br>
 4<br>
 5<br>
 6<br>
 7<br>
 8<br>
 9 (uh oh, I’m getting full!)<br>
10 (ticked over - start a new digit)</p>

<p>What if we ticked over at 60 when we counted, like we do for seconds and minutes?</p>

<p>1 second<br>
2<br>
3<br>
4<br>
5<br>
…<br>
58<br>
59<br>
1:00 (60 seconds aka 1 minute. We’ve started a new digit.)</p>

<p>Everything OK so far, right? Note that we use the colon (:) indicate that we are at a new “digit”. In base 10, each digit can stand on its own.</p>

<h2>Try Base 16</h2>

<p>If we want base 16, we could do something similar:</p>

<p>1<br>
2<br>
3<br>
4<br>
5<br>
6<br>
7<br>
8<br>
9<br>
10<br>
11<br>
12<br>
13<br>
14<br>
15 (we’re getting full)<br>
1:00 (16 - we’ve started a new digit)</p>

<p>However, we don’t want to write hexadecimal numbers with the colon notation (even though we could). We’d rather cook up separate symbols for 10-15 so we can just write numbers like we’re used to. We’ve run out of numbers (1-9 already used, with 0 as a placeholder) so we need some other symbols. We could use some squiggly lines or other shapes, but the convenions is to use letters, Roman style. Just like 5 became V, programmers use letters A-F to get enough digits up to 16. That is,</p>

<p>1<br>
2<br>
3<br>
4<br>
5<br>
6<br>
7<br>
8<br>
9<br>
A (10 - we’re using the symbol “A”)<br>
B (11)<br>
C (12)<br>
D (13)<br>
E (14)<br>
F (15 - uh oh, we’re getting full)<br>
10 (16 - we start a new digit)</p>

<p>Ahah! Now we can use one digit per “place”, and we know that 10 actually means we’ve “ticked over to 16″ once.</p>

<p>20 means we’ve ticked over to 16 twice (32).</p>

<p>25 means we’ve ticked over to 16 twice (giving us 32) and gone an extra 5. The total is 32 + 5 = 37.</p>

<h2>Quick review</h2>

<p>With me so far? This is pretty cool, right? We can count in any system we want. Also notice that base 16 is more “space efficient” in the sense we can write a number like 11 in a single digit: B.</p>

<p>Base 16 really isn’t that different from base 10, we just take longer to fill up.</p>

<h2>The wonderful world of binary</h2>

<p>We’ve seen plenty of base systems, from over-simple unary, to the unweidly Roman numerals, the steady-going base 10 and the compact base 16.</p>

<p>What’s great about binary? In the spirit of keeping things simple, it’s the simplest number system that has the concept of “ticking over”. Unary, where we just write 1, 11, 111… just goes on forever. Binary, with two options (1 and 0) looks like this:</p>

<p>1: 1<br>
2: 10 (we’re full - tick over)<br>
3: 11<br>
4: 100 (we’re full again - tick over)<br>
5: 101<br>
6: 110<br>
7: 111<br>
8: 1000 (tick over again)<br>
…</p>

<p>and so on.</p>

<p>Because binary is so simple, it’s very easy to build in hardware. You just need things that can turn on or off (representing 1 and 0), rather than things that have 10 possible states (to represent decimal).</p>

<p>Because it’s so simple, binary is also resistant to errors. If your signal is “partially on” (let’s say 0.4), you can assume that’s a zero. And if it’s mostly on (say 0.8), then you can assume it’s a 1. If you’re using a system with 10 possible states, it’s difficult to tell when an error occurred. This is one reason digital signals are so resilient to noise.</p>

<h2>Other examples of bases</h2>

<p>We use other bases all the time, even dynamically changing bases. We usually don’t think of it that way:</p>

<p>Hours, minutes, seconds: 1:32:04</p>


<ul>
<li>We know this is 1 hour, 32 minutes, 4 seconds. In seconds, this is 1*60*60 + 32*60 + 4.</li>
</ul>



<p>Feet and inches: 3′ 5″</p>


<ul>
<li>This is 3 feet, 5 in or 3 * 12 + 5 inches.</li>
</ul>



<p>Pounds and ounces: 8 lbs, 5 oz</p>


<ul>
<li>Since a pound is 16 oz, This is 8 * 16 + 5 oz. We’ve been using a base 16 number system all along!</li>
</ul>



<h2>Parting thoughts</h2>

<p>“10″ in any number system indicates the base, and means we’ve ticked over once. 10 in binary means two, 10 in decimal means ten, and 10 in hexadecimal is sixteen. </p>

<p>How do you keep these numbers apart? Programmers will often write “0b” in front of binary numbers. So 2 in binary is</p>

<p>0b10</p>

<p>Similarly, they’ll write 0x in front of hex numbers. So 16 in hex is:</p>

<p>0×10</p>

<p>If there aren’t any symbols (0b or 0x) in front, we assume it’s base 10, a regular number.</p>

<p>Now go forth and enjoy your new knowledge! </p></div>
</div>

<div class="feed">
  <div class="title">9. <a href="http://feeds.feedburner.com/~r/Betterexplained/~3/74615342/" target="_blank">Types of Graphs</a></div>
  <div class="content"><p> I hate to break it to you, but all graphs are not created equal. I realized this recently, wrote an article, and had my computer crash. The article vanished, but I’m determined to get this idea on the web. It’s a simple but interesting concept.</p>

<h2>Types of Graphs</h2>

<p>Let’s start with the types of graphs. Just as the universe can be divided into “bananas” and “non-bananas”, I’m going to divide graphs into “map” and “non-map” graphs.</p>


<ul>
<li>“Map graphs” are ones where the <strong>x and y axes correspond to distance:</strong> feet, meters, whatever you want. Thus, an x-y coordinate represents a location of some sort. </li>
</ul>




<ul>
<li>“Non-map graphs”, or all other graphs, are simply graphs where the coordinates do not imply distance, and instead the <strong>axes indicate production, time</strong> or some other variable. Some non-map graphs are <strong>hybrid graphs</strong> where one axis is distance and the other is not.</li>
</ul>



<p>We’ll see that these graphs types are not the same and it’s important to know the difference.</p>

<h2>Graph Examples</h2>

<p>Consider this typical example of a map graph:</p>

<p><img src="http://betterexplained.com/wp-content/uploads/graphs/map_graph_regular.gif" alt="map_graph_regular.gif" title="map_graph_regular.gif" width="285" height="242" border="0"></p>

<p>Suppose we want to get from the proverbial point A (1, 1) to point B (4, 5). Let’s say there’s some cost associated with the trip: time spend, gas used, or any other expense you want. Our goal is to minimize this expense by driving as little as possible.</p>

<p>We know the shortest distance between two points is a straight line. Using Pythagoras’ theorem, we figure out this distance is 5 units (making a 3-4-5 right triangle). </p>

<p>If we decided to drive along in a triangle, going straight across to (4,1) and then from (4,1) to B, it would be a distance of 7 units. Obviously, going from A to B directly is the least expensive path.</p>

<p>Now consider this typical “non-map” graph showing the number of cars and trucks at a rental company:</p>

<p><img src="http://betterexplained.com/wp-content/uploads/graphs/purchase_graph_paths.gif" alt="purchase_graph_paths.gif" title="purchase_graph_paths.gif" width="285" height="242" border="0"></p>

<p>Suppose the company wants to increase its inventory. Currently it is at point A, with one car and one truck. It wants to get to point B (4 cars, 5 trucks). The cost of the “trip” is simply the cost of buying the new cars and trucks. How should it buy its vehicles?</p>

<p>This isn’t a very hard problem. We can see that no matter what “path” they take to B, the cost is the same — they end up buying 3 cars and 4 trucks. They can buy all their cars then all their trucks, vice-versa, or anything in between. They can even buy more cars and trucks than they need and sell them (assuming they can get the same price), which gives us the windy path. If they buy all of one type and then the other, they get a path shaped as a triangle.</p>

<p>Now, why are these two examples different? The simple reason is that <strong>distance does not matter in non-map graphs</strong>. Quite simply, if a map doesn’t have distance on its axes, then “distance” in the graph does not matter at all. Pythagoras’ theorem does not help us. Knowing that the shortest distance between two points is a line does not help us. We can’t escape the fact that we must end up with 4 cars and 5 trucks. But is this the end of the story?</p>

<h2>Nope, it’s not the end of the story</h2>

<p>It appears that in map graphs we want to take a direct route. In non-map graphs, we are stuck and can’t take a shortcut. Now consider this map graph, which shows terrain like mountains:</p>

<p><img src="http://betterexplained.com/wp-content/uploads/graphs/map_graph_mountains.gif" alt="map_graph_mountains.gif" title="map_graph_mountains.gif" width="285" height="242" border="0"></p>

<p>We still want to get from A to B, but now we must cross the mountains to do so. Suppose that in the mountains, the cost of traveling is 10 times normal. Suddenly the direct route doesn’t make as much sense. Taking the triangle route along the sides (from A to (4,1) to B) will be cheaper.</p>

<p><strong>The lesson:</strong> On distance graphs where the cost varies from point to point, the cheapest path may no longer be a straight line. This enters the realm of math and physics problems, where you are measuring distance, but there is another effect (like a field) that changes the cost from point to point. Future articles will talk about this more.</p>

<h2>Advanced example: Light waves</h2>

<p>A great example of a hybrid graph (distance on one axis but not the other) is how light waves are usually portrayed. Here’s an example of a <a href="http://en.wikipedia.org/wiki/Image:Wave.png">wave</a> from Wikipedia:</p>

<p><img src="http://betterexplained.com/wp-content/uploads/graphs/Wave.png" alt="Wave.png" title="Wave.png" width="554" height="273" border="0"></p>

<p>It’s a great image, but has the potential for confusion. Light waves don’t travel up and down like a rollercoaster, but it’s very easy to think that after looking at the graph.</p>

<p>In fact, what the graph is saying is that as the wave moves along, its <strong>amplitude</strong> (strength) varies. The light itself can take the same path. To show this, I added a line to the graph where the brightness (amplitude) varies:</p>

<p><img src="http://betterexplained.com/wp-content/uploads/graphs/wave_amplitude_line.png" alt="wave_amplitude_line.png" title="wave_amplitude_line.png" width="531" height="275" border="0"></p>

<p>Notice how the brightness of the bar corresponds to the amplitude of the wave. At the peaks it is white (full brightness = max strength), and in the valleys it is black (least brightness = least strength). That is the concept the graph is trying to convey, but it can be tough to get our brain to look at the graph and not think of a wave as wiggling up and down like a roller-coaster. </p>

<p>Sure, waves <strong>can</strong> wiggle up and down (just like most everything else in the universe), but that’s not the point of the diagram. The point is that the <strong>strength</strong> wiggles up and down, even for things moving in a straight line.</p>

<p>For you science geeks, yes, the true and mysterious nature of light appears to be an interesting particle-wave duality, where the strength of a particle varies with distance and shows other wave properties, but just hang with me for the sake of example.</p>

<h2>Intuition</h2>


<ul>
<li>Distance matters on map-graphs. Although a straight line may be the shortest path, it’s not necessarily the “cheapest”. Be careful.</li>
</ul>




<ul>
<li>On other maps, distance on the graph doesn’t mean location. The axes don’t measure distance, so they paths along the graph don’t correspond to locations travelled. It’s easy to confuse objects taking winding paths on the graph as taking a winding path in the real world. </li>
</ul></div>
</div>

<div class="feed">
  <div class="title">10. <a href="http://feeds.feedburner.com/~r/Betterexplained/~3/74615341/" target="_blank">Gmail contacts flaw: Overview and suggestions</a></div>
  <div class="content"><p> There’s <a href="http://reddit.com/info/wlvq/comments">news and discussion</a> about a recent flaw in Gmail that can expose your contact list to any page.</p>

<p><strong>Summary:</strong> If you are logged into a Google account (email, personal homepage, etc) then another site can use that authentication to access your contact list. Log out before visiting sites you semi-trust.</p>

<p>This is an example of <a href="http://en.wikipedia.org/wiki/Cross-site_request_forgery">Cross Site Request Forgery</a> - you learn something new every day. There’s a <a href="http://getahead.ltd.uk/blog/joe/2007/01/01/csrf_attacks_or_how_to_avoid_exposing_your_gmail_contacts.html">detailed writeup here</a> on the gmail flaw.</p>

<p><strong>Update (1/1/06):</strong> The flaw appears to be fixed.</p>

<h2>How it works</h2>

<p>The <a href="http://www-static.cc.gatech.edu/~achille/contacts-source.txt">code</a> is pretty straightforward. Basically, Google docs has a script that runs a callback function, passing it your contact list as an object. The script presumably checks a cookie to ensure you are logged into a Google account before handing over the list.</p>

<p>Unfortunately, it doesn’t check what <strong>page</strong> is making the request. So, if you are logged in on window 1, window 2 (an evil site) can  make the function call and get the contact list as an object. Since you are logged in somewhere, your cookie is valid and the request goes through.</p>

<p>Also, if you check the object that is returned, you see fields for the contact’s name, email and “affinity”. Presumably, a higher affinity means a more-emailed contact, so it may be possible to know the relative importance of your contacts.</p>

<h2>Possible solutions</h2>

<p>Google is run by smart people and I’m sure they’ll have this fixed soon. A few suggestions appear to be popping up, all centered on making sure the user is on a Google.com page and not a random site:</p>


<ul>
<li>Referrer blocking: Block all requests from sites not in the google.com domain. However, some people run referrer-blocking software. It may be the price they have to pay for security, but there could be other consequences.</li>
</ul>




<ul>
<li>Script checks: An idea I had was to check the window.location (just like you check the cookie) to make sure it’s coming from a google.com domain. This is another way to see what page is making the request.</li>
</ul>




<ul>
<li>Challenge-response: Google pages (like Gmail) can have some token or unique, computed data that they submit with their requests. Random pages won’t have access to this token when they make the function call. The above solution works, but can be cumbersome since it has to be added to every form field.</li>
</ul>




<ul>
<li>(From user <span>JRF </span>on reddit): Include part of cookie in the request <span>URL </span>as a unique token that only a “real” Google page would know. Need to watch out for proxies/browser history (accessible from other pages) being able to access this unique data. May need to seed or salt it in a challenge-response system. This is known as “double-submitting” the cookie - the server can check that your cookie actually contains the value you submitted (the evil page can’t access your cookie directly, only the fact that you are logged in).</li>
</ul>



<p>It’s interesting to think of solutions to this problem - do you have any others?</p>

<p>I’m sure Google will have this fixed, but beware browsing random sites on the net (obvious, yes, but be especially wary). Log out of Google first.</p>

<p>This is a wake-up call about the realities of Web security.</p>

<h2>Other Resources</h2>


<ul>
<li><a href="http://shiflett.org/articles/cross-site-request-forgeries"><span>CSRF</span> Explanation &amp; <span>PHP</span> Code</a> - has examples of token authentication using <span>PHP. </span></li>
</ul></div>
</div>

<div class="feed">
  <div class="title">11. <a href="http://feeds.feedburner.com/~r/Betterexplained/~3/74615340/" target="_blank">Collected Notes on Success</a></div>
  <div class="content"><p> This is a collection of thoughts, my own and from the web, on success. The list will grow as I do <img src="http://betterexplained.com/wp-includes/images/smilies/icon_smile.gif" alt=":)"> </p>

<h2>Richard St. John (<a href="http://www.ted.com/tedtalks/tedtalksplayer.cfm?key=r_stjohn">Success Video from <span>TED</span> Conference</a>)</h2>

<p>This was a 3-minute video outlining 8 factors leading to success. I organized them into buckets:</p>


<ul>
<li>Do what you love: Passion</li>
<li>Determination: Hard work, focus, push yourself, get good</li>
<li>Make something useful: Ideas, serve others</li>
<li>Keep your chin up: Persist</li>
</ul>



<p>I really want to highlight the last point: persist in the face of failure and other <span>CRAP</span>:</p>


<ul>
<li>Criticism</li>
<li>Rejection</li>
<li>Jerks (or another word for a non-nice person)</li>
<li>Pressure</li>
</ul>



<p>This video was also a great example of the 80/20 rule. In 3 minutes you got the gist of the presentation. Would it have been 10-times better if it was 30 minutes long? No.</p>

<p>Can you get more from ten 3-minute videos instead of one 1/2 hour video? Probably.</p>

<p>An unmentioned part of success may be realizing when you’ve hit the point of diminishing returns and knowing when to move on.</p>

<h2>Steve Jobs (Stanford graduation speech and video)</h2>

<p><embed src="http://www.youtube.com/v/D1R-jKKp3NA" width="425" height="350" allowScriptAccess="never" wmode="transparent" type="application/x-shockwave-flash"></embed></p>

<p>Selected quotes from the <a href="http://news-service.stanford.edu/news/2005/june15/jobs-061505.html">Full Transcript</a>:</p>


<ul>
<li>You’ve got to find what you love.</li>
<li>Stay Hungry. Stay Foolish.</li>
<li><strong>Your time is limited, so don’t waste it living someone else’s life.</strong> Don’t be trapped by dogma - which is living with the results of other people’s thinking. Don’t let the noise of others’ opinions drown out your own inner voice. And most important, have the courage to follow your heart and intuition. They somehow already know what you truly want to become. Everything else is secondary.</li>
<li><strong>Remembering that I’ll be dead soon is the most important tool I’ve ever encountered to help me make the big choices in life.</strong> Because almost everything - all external expectations, all pride, all fear of embarrassment or failure - these things just fall away in the face of death, leaving only what is truly important. Remembering that you are going to die is the best way I know to avoid the trap of thinking you have something to lose. You are already naked. There is no reason not to follow your heart.
<ul>
<li>Note: Very true. It’s human nature, myself included, to not appreciate things until they are gone (or have a credible threat of being taken). </li>
</ul>
</li>
</ul></div>
</div>

<div class="feed">
  <div class="title">12. <a href="http://feeds.feedburner.com/~r/Betterexplained/~3/74615339/" target="_blank">Using JSON to Exchange Data</a></div>
  <div class="content"><p> Javascript Object Notation (JSON) is a fancy name for a simple idea: A web page can download data stored as javascript variables. It’s caused a buzz in the tech world because <span>JSON </span>is much easier to load, read and manipulate compared to <span>XML.</span> Also, parsing large <span>XML </span>files can be a large performance hit - <span>JSON </span>gives you an object, packaged up and ready to go!</p>

<h2>Remember the script tag?</h2>

<p>In javascript you can import any script using notation like this:</p>



<pre>
<code>&lt;script src=&quot;http://www.mysite.com/myscript.js&quot;&gt;&lt;/script&gt;</code>
</pre>



<p>The browser will pull down the script and run it inside the current page. This is how embeddable widgets like Adsense and <a href="http://instacalc.com">InstaCalc</a> work.</p>

<p>The neat thing is that any variables and functions defined inside the script are available to the page. If myscript.js includes a function showMeTheMoney(), your page can call it (and, presumably, get shown the money).</p>

<p>This is all fine and dandy, and been known for a while. A cool use is to provide <strong>dynamic data</strong> to a page with encoded Javascript variables. If the variables were “objects” and we passed them using their special “notation”, we could call the system <strong>JavaScript Object Notation</strong>.</p>

<p>In Javascript, you can use objects like this:</p>


<pre>
<code>var fruit = {}; // create new obj
fruit.name = "apple"; // set properties
fruit.color = "red";

// that is old fashioned! Try my new notation:

var fancyFruit = {"name":"pear", "color": "greenish"};
</code>
</pre>



<p>Javascript has a nice notation where you can define objects using <code>"key":"value"</code> pairs. You can define arrays and functions this way too, so your objects are more like a class, in fact.</p>

<h2>So why is this useful again?</h2>

<p>So far, passing scripts around is old news. But an idea comes along: what if you load a <strong>dynamically generated</strong> script, which has data inside of it? Now you can access the data as javascript variables. Here’s what I mean:</p>



<pre>
<code>&lt;script src=&quot;http://www.weathersite.com/latestweather.js&quot;&gt;&lt;/script&gt;
Today&#39;s weather is:
&lt;script&gt;
document.write(Weather.today);
&lt;/script&gt;</code>
</pre>



<p>See what’s happening here? We load the javascript (which could be updated every hour by the server), and access the Weather object it defines. Presumably, Weather.today is a string containing today’s weather. Realistically, we could have Weather[zipcode] or whatever format the site defines.</p>

<p>Cool, huh?</p>

<h2>But why not <span>XML</span>?</h2>

<p>Ah — some programmers will say “Verily, can you not download an <span>XML </span>file, and then process and parse it into Javascript variables?”</p>

<p>Sure, just like you <strong>can</strong> run a marathon or floss your teeth. Many want to, some try to, not everybody does.</p>

<p><span>XML </span>is fine for certain things, but it can be quite cumbersome. <span>JSON </span>is great because you can include data in an easy, painless process. There’s no parsing step - you are getting your variables “for free” by just including the javascript file.</p>

<p>Also, <span>JSON </span>lets the site include <strong>functions</strong>, which may be used to process the data or handle other tasks.</p>

<h2>A few details (as always)</h2>

<p>There are various ways to use <span>JSON </span>(see links for source of code sample):</p>


<ul>
<li>Include a script tag directly (as above). This means the code inside the script is run immediately as the browser encounters it.</li>
</ul>





<pre>
<code>&lt;script src=&quot;http://www.mysite.com/mydata.js&quot;&gt;&lt;/script&gt;</code>
</pre>




<ul>
<li><a href="http://www.codehouse.com/javascript/articles/external/">Dynamically load the script tag</a>. You can add a javascript tag to the <code>head</code> of a document (from codehouse.com):</li>
</ul>





<pre>
<code>function dhtmlLoadScript(url)
{
   var e = document.createElement("script");
   e.src = url;
   e.type="text/javascript";
   document.getElementsByTagName("head")[0].appendChild(e); 
}
dhtmlLoadScript("http://www.mysite.com/mydata.js");</code>
</pre>





<ul>
<li>Use <span>AJAX </span>(Xml Http Request) to pull some text from the server and then <code>eval</code> it. Note that the first two techniques automatically “eval” the javascript because it is inside a script tag. You probably want to use a library to do the <span>XHR </span>request (like <a href="http://ajaxpatterns.org/XMLHttpRequest_Call#AjaxCaller">AjaxCaller</a>):</li>
</ul>





<pre>
<code>ajaxCaller.getPlainText("mydata.js", 
   function(jsText) { eval(jsText); });
</code>
</pre>



<p>Note that <span>XHR </span>requests can only open files on the same domain as the caller. The first two techniques can load a file from any domain.</p>

<h2>Security, Security, Security</h2>

<p>I’m not an expert in Web Security, but I know enough to realize it’s tricky and that there will be things I miss. Even the big boys like Google can get <a href="http://betterexplained.com/articles/gmail-contacts-flaw-overview-and-suggestions/">caught in bugs sometimes</a>.</p>

<p>The safest approach is to only store public data with <span>JSON.</span> If you store private data in <span>JSON </span>(or <span>XML </span>for that matter), there are techniques where malicious sites can impersonate logged-on users. Sure, there are techniques like double-submitting cookies to avoid this, but if you’re new (like me), keep your <span>JSON </span>data to innocuous things at first (like weather reports).</p>

<h2>Callbacks</h2>

<p>The <span>JSON </span>data may not be a raw object; it could be an object passed to a callback function, such as:</p>



<pre>
<code>specialFunction( {"name" : "Bob", "email": "bob@example.com"} );
</code>
</pre>



<p>In this case, the script will run a function called <code>specialFunction</code> that you’ve defined, passing it an anonymous object with a “name” and “email” property. Your <code>specialFunction</code> can then do wild things with this information.</p>

<p>This technique is the same at heart - this is how Google originally passed your Gmail contacts. Sometimes you can specify the name of your callback function in the <span>URL </span>you use to access the <span>JSON </span>data - it depends on the data provider.</p>

<h2>Real-life example: Currency conversion in InstaCalc</h2>

<p>I recently added <a href="http://instacalc.com/blog/currency-conversions-and-flexible-units">currency conversion to the InstaCalc Online Calculator</a>, which was a good lesson in importing data:</p>


<ul>
<li>Find your data source. The <a href="http://www.newyorkfed.org/markets/fxrates/noon.cfm">Federal Bank of New York</a> has an <span>XML </span>file with currency conversion data. That’s good enough for me!</li>
</ul>




<ul>
<li>Convert data to <span>JSON </span>format. You can convert <span>XML </span>to <span>JSON </span>using <span>XSLT </span>transformations.</li>
</ul>




<ul>
<li><a href="http://xml.apache.org/xalan-j/">Install <span>XALAN</span></a> so you can do <span>XSL </span>transformations using Java. A typical transformation looks like this:</li>
</ul>





<pre>
<code>java org.apache.xalan.xslt.Process -classpath &quot;xalan.jar&quot; 
-IN in.xml -XSL in.xslt &gt; out.xml</code>
</pre>



<p>Be careful for Java classpath issues, I had trouble and had to include the classpath to xalan.jar manually.</p>


<ul>
<li>Convert <span>XML </span>attributes to elements. Attributes can be difficult to access in the various <span>XML </span>to <span>JSON </span>scripts, so make them elements. Use this <span>XSL </span>to convert attributes to elements:</li>
</ul>




<pre>
<code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;ISO-8859-1&quot;?&gt;
&lt;xsl:stylesheet xmlns:xsl=&quot;http://www.w3.org/1999/XSL/Transform&quot;
   version=&quot;1.0&quot;&gt;
  &lt;xsl:output indent=&quot;yes&quot; /&gt;
  &lt;xsl:strip-space elements=&quot;*&quot; /&gt;
  &lt;xsl:template match=&quot;*&quot;&gt;
    &lt;xsl:copy&gt;
      &lt;xsl:if test=&quot;@*&quot;&gt;
        &lt;xsl:for-each select=&quot;@*&quot;&gt;
          &lt;xsl:element name=&quot;{name()}&quot;&gt;
            &lt;xsl:value-of select=&quot;.&quot; /&gt;
          &lt;/xsl:element&gt;
        &lt;/xsl:for-each&gt;
      &lt;/xsl:if&gt;
      &lt;xsl:apply-templates /&gt;
    &lt;/xsl:copy&gt;
  &lt;/xsl:template&gt;
&lt;/xsl:stylesheet&gt;</code>
</pre>



<p>Then process the <span>XML </span>file using <span>XALAN</span>:</p>



<pre>
<code>java org.apache.xalan.xslt.Process -classpath &quot;xalan.jar&quot; 
-IN in.xml -XSL attributes-to-elements.xslt &gt; no-attributes.xml</code>
</pre>




<ul>
<li>Finally, convert the <span>XML </span>to <span>JSON </span>using this <a href="http://code.google.com/p/xml2json-xslt/"><span>XSL </span>transformation</a>. There are several <span>XML</span> 2 <span>JSON </span>stylesheets out there if you search.</li>
</ul>



<p>Run your <span>XML </span>through <span>XALAN </span>to create the final <span>JSON </span>data.</p>



<pre>
<code>java org.apache.xalan.xslt.Process -classpath &quot;xalan.jar&quot; 
-IN no-attributes.xml -XSL xml2json.xslt &gt; currencies.json</code>
</pre>




<ul>
<li>Clean up your data. You have the raw <span>JSON </span>data as an object, but you may wish to wrap it in a function call. You can use a quick perl script to wrap “myFunction( )” around the data above so your file does a callback when executed.</li>
</ul>



<p>Tada! Now you have <span>JSON </span>data ready to access using one of the techniques above (<a href="http://instacalc.com/data/currencies.json">see the currency <span>JSON </span>data</a>). Inside InstaCalc, I have a static reference to the file:</p>


<pre>
<code>
&lt;script src=&quot;http://instacalc.com/data/currencies.json&quot;&gt;&lt;script&gt;
</code>
</pre>



<p>Because the bank has some namespace information, I access variables like this inside the callback function: <code>data["frbny:DataSet"];</code>. Of course, the details of how you access your <span>JSON </span>data will change given the format of your <span>XML </span>and the exact stylesheet you used. Play around. If you look at the raw <span>JSON </span>file you can see the field names you need to use.</p>

<p><strong>Bonus round: Updating the <span>JSON.</span></strong> If your data changes often (like currency data does), you can put the above steps into a script and run it on a schedule. The next time your webpage is loaded, it will get the new <span>JSON </span>data.</p>

<h2>Parting thoughts</h2>

<p><span>JSON </span>is a really easy way to exchange data - just think of it as including extra javascript files in your program. Read more at <a href="http://json.org">json.org</a>, learn it, and love it. Good luck. </p></div>
</div>

<div class="feed">
  <div class="title">13. <a href="http://feeds.feedburner.com/~r/Betterexplained/~3/76108729/" target="_blank">Swap two variables using XOR</a></div>
  <div class="content"><p> Most people would swap two variables x and y using a temporary variable, like this:</p>

<p><code>tmp = x</code><br>
<code>x = y</code><br>
<code>y = tmp</code></p>

<p>Here’s a neat programming trick to swap two values without needing a temp:</p>



<pre>
<code>x = x xor y
y = x xor y
x = x xor y</code>
</pre>



<p>Don’t believe me? Try it out - write in any initial value for x and y:</p>

<p></p>

<p>It almost seems like magic - the the same statement is repeated 3 times and <em>voila</em> - the values magically swap? Let’s take a closer look.</p>

<h2>How it works</h2>

<p>To understand this trick, break the statements into unique values:</p>



<pre>
<code>x1 = x xor y
y1 = x1 xor y
x2 = x1 xor y1</code>
</pre>



<p>According to our code, x2 should have y’s original value. Let’s work out the details for the final equation:</p>



<pre>
<code>x2 = x1 xor y1
x2 = x1 xor (x1 xor y) // replace y1
x2 = (x1 xor x1) xor y // regroup parenthesis - order does not matter for XOR
x2 = 0 xor y            // a xor a == 0
x2 = y                   // a xor 0 == a; x2 = y's original value</code>
</pre>



<p>Wow - x2 really does equal y! The swap happened. Now let’s try it out for y1:</p>



<pre>
<code>y1 = x1 xor y
y1 = (x xor y) xor y
y1 = x xor (y xor y)
y1 = x xor 0
y1 = x // y1 == x's original value</code>
</pre>



<p>And tada the trick worked again. x2 and y1 have the swapped answers.</p>

<h2>Intuitive Understanding</h2>

<p>Ok, sure, the boolean algebra works out great — but that’s not satisfying. I want to <strong>understand</strong> it deep down and have it make sense, not be some artifact of the properties of <span>XOR.</span> Let’s take another look:</p>



<pre>
<code>1. x = x xor y
2. y = x xor y
3. x = x xor y</code>
</pre>



<p>On line 1 we combine x and y (using <span>XOR</span>) to get this “hybrid” and we store it back in x. <span>XOR </span>is a great way to save information, because you can remove it by doing an <span>XOR </span>again.</p>

<p>So, this is exactly what we do on line 2. We <span>XOR </span>the hybrid with y, which cancels out all the y information, leaving us only with x. We save this result back into y, so now they have swapped.</p>

<p>On the last line, x still has the hybrid value. We <span>XOR </span>it yet again with y (now with x’s original value) to remove all traces of x out of the hybrid. This leaves us with y, and the swap is complete!</p>


<h2>Would you really use this?</h2>

<p>No way. This is a cool trick, but don’t write this as an actual swap function, If you have to debug it in 6 months you’ll be in for some fun. Let me show you why:</p>

<p>Suppose x and y are pointers or references to objects, and both point to the same location. We’d expect our swapping function to just switch them around, with no change, right?</p>

<p>Well, take a look at what happens if we expand out line 1:</p>



<pre>
<code>
x = x xor y
x = x xor x  // x and y are equal
x = 0
</code>
</pre>



<p>Wow! So x becomes 0 right at the get-go. That’s ok by itself, but because x and y are at the same location, we just made y zero as well! We’ve lost the original values, a problem known as <a href="http://en.wikipedia.org/wiki/Aliasing_%28computing%29">aliasing</a>: changing one variable has an indirect effect on another.</p>

<p>So, would you have caught this bug? I wouldn’t have, and it would have been a nightmare figuring out why an innocuous swap function was causing data loss. Cute tricks like these can be pretty dangerous. As Brian Kernighan said:</p>

<blockquote><p>Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.</p></blockquote>

<p>This applies so well here - we wrote the code pretty much as cleverly as we could <img src="http://betterexplained.com/wp-includes/images/smilies/icon_smile.gif" alt=":)"> . So, treat this as an intellectual exercise that brings up a few points:</p>


<ul>
<li><span>XOR </span>is a great way to combine information and later extract it. <span>XOR</span>-based encryption uses this technique. Also, <span>XOR </span>can combine N items together, not just 2.</li>
<li>There are new ways to perform even the simplest operations.</li>
</ul>



<h2>Even more hairy details</h2>

<p>Now, how does this work on a <span>CPU</span>-level?</p>

<p>The computer actually has an implicit “temp” variable that stores intermediate results before writing them back to a register. For example, if you add 3 to a register (in machine-language pseudocode):</p>

<p><code>ADD 3 A  // add 3 to register A</code></p>

<p>The <a href="http://en.wikipedia.org/wiki/Arithmetic_logic_unit"><span>ALU</span></a> (Arithmetic Logic Unit) is actually what executes the instruction 3+A. It takes the inputs (3,A) and creates a result (3 + A), which the <span>CPU </span>then stores back into A’s original register. So, we used the <span>ALU </span>as temporary scratch space before we had the final answer.</p>

<p>We take the <span>ALU’</span>s implicit temporary data for granted, but it’s always there. In a similar way, the <span>ALU </span>can return the intermediate result of the <span>XOR </span>in the case of <code>x = x xor y</code>, at which point the <span>CPU </span>stores it into x’s original register.</p>

<p>Because we aren’t used to thinking about the poor, neglected <span>ALU, </span>the <span>XOR </span>swap seems magical because it doesn’t have an explicit temporary variable.  Some machines have a 1-step exchange <span>XCHG </span>instruction to swap two registers.</p>

<p>Further reading</p>


<ul>
<li><a href="http://en.wikipedia.org/wiki/XOR_swap_algorithm">Wikipedia article on <span>XOR </span>swap</a></li>
<li><a href="http://www.delphi3000.com/articles/article_2534.asp?SK=">Forum post with assembly examples</a> </li>
</ul></div>
</div>

<div class="feed">
  <div class="title">14. <a href="http://feeds.feedburner.com/~r/Betterexplained/~3/80104729/" target="_blank">Mental Math Shortcuts</a></div>
  <div class="content"><p> Here’s a collection of time-saving math shortcuts, great for back-of-the-envelope estimates.</p>

<h2>Time and Distance</h2>

<p><strong>60 mph = 1 mile per minute</strong></p>


<ul>
<li>Going 60 mph and the exit is in 10 miles? That’s 10 minutes.</li>
<li>Been driving a half hour? That’s about 30 miles at highway speeds.</li>
</ul>



<p><strong>Feet Per Second = <span>MPH </span>* 1.5</strong><br>
<strong><span>MPH </span>= Feet Per Second * 2/3</strong> (<a href="http://instacalc.com/?d=&amp;%23038;c=Ly8gbXBoIGludG8gZmVldCBwZXIgc2Vjb25kfG1waCA9IDUwfGZwcyA9IG1waCAqIDUyODAgLyAoNjAgKiA2MCkgLy8gZXhhY3QgYW5zd2VyfGZwcyA9IDEuNSAqIG1waCAvLyBwcmV0dHkgY2xvc2UuLi58fHw&amp;%23038;s=sssssss&amp;%23038;v=0.8">derivation</a>)</p>


<ul>
<li>60 mph is about 90 feet per second (88 exactly), so just multiply by 1.5.</li>
<li>Going 100 mph? That’s 150 fps.</li>
<li>Going 10 fps? That’s about 7 mph (10 * 2/3 is 6.666).</li>
</ul>



<p><strong>speed of light = 1 foot per nanosecond</strong> (<a href="http://instacalc.com/?d=&amp;%23038;c=Ly8gc3BlZWQgb2YgbGlnaHQgaW50byBmZWV0IHBlciBzZWNvbmR8YyA9IDMwMGsgLy8ga20vc2Vjb25kfGZwcyA9IGMgKiAuNiAqIDUyODAgLyAxIGJpbGxpb24gLy8gZmVldC9uYW5vc2Vjb25kIChleGFjdCl8ZnBzID0gMSAvLyBjbG9zZSBlbm91Z2ghfHx8&amp;%23038;s=sssssss&amp;%23038;v=0.8">derivation</a>)</p>


<ul>
<li>The US is about 3000 miles long. There’s about 5000 feet/mile, so that’s about 3000 × 5000 or 15 million feet. 15 million feet takes 15 million nanoseconds, or 15 / 1000, or 15 milliseconds. That’s the minimum latency for a signal across the country.</li>
<li>Inside a microchip, if you have a clock cycle every nanosecond (1 GHz), your signal can only travel 1 foot. Distances matter.</li>
</ul>



<h2>Money and Finance</h2>

<p><strong>$1/hour = $2000/year</strong> (<a href="http://instacalc.com/?d=&amp;%23038;c=Ly8gZG9sbGFycy9ob3VyIGluIGRvbGxhcnMveWVhcnx3YWdlID0gMSAvLyBkb2xsYXJzL2hvdXJ8d2Vla3MgPSA1MHxob3VycyA9IDQwfHllYXJseSA9IHdhZ2UgKiB3ZWVrcyAqIGhvdXJzfHw&amp;%23038;s=sssssss&amp;%23038;v=0.8">derivation</a>)</p>


<ul>
<li>Earn $25/hour? That’s about 50k/year.</li>
<li>Make 200k/year? That’s about $100/hour. This assumes a 40 hour week.</li>
</ul>



<p><strong>$20/week = $1000/year</strong> (<a href="http://instacalc.com/?d=&amp;%23038;c=Ly8gZG9sbGFycy93ZWVrIGluIGRvbGxhcnMveWVhcnx3ZWVrbHlfc3BlbmRpbmcgPSAyMHx3ZWVrcyA9IDUwfHllYXJseSA9IHdlZWtseV9zcGVuZGluZyAqIHdlZWtzfHx8&amp;%23038;s=sssssss&amp;%23038;v=0.8">derivation</a>)</p>


<ul>
<li>Spend $20/week at Starbucks? That’s a cool grand a year.</li>
</ul>



<p><strong>Rule of 72: Years To Double = 72/Interest Rate</strong> (<a href="http://instacalc.com/?d=&amp;%23038;c=Ly8gcnVsZSBvZiA3MnxyYXRlID0gNiAvLyBwZWNlbnR8eWVhcnNfdG9fZG91YmxlID0gNzIvcmF0ZSAvLyBhcHByb3hpbWF0ZXxleGFjdCA9IGxvZygyKSAvIGxvZygxICsgcmF0ZS8xMDApIC8vIGV4YWN0fHx8&amp;%23038;s=sshhsss&amp;%23038;v=0.8">derivation</a>)</p>


<ul>
<li>Have an investment growing at 10% interest? It will double in 7.2 years.</li>
<li>Want your investment to double in 5 years? You need 72/5 or about 15% interest.</li>
<li>Growing at 2% a week? You’ll double in 72/2 or 36 weeks. You can use this rule for any duration of time, not just years. </li>
<li>Inflation at 4%? It will halve your money in 72/4 or 18 years.</li>
</ul>



<h2>Mental Arithmetic</h2>

<p><strong>10,000 = hundred hundred</strong><br>
<strong>million = thousand thousand</strong><br>
<strong>billion = thousand million</strong></p>


<ul>
<li>1% of 10k is 100. The Dow is roughly 10k (it’s about 12k now). So if the dow drops 100, it’s about a 1% loss.</li>
<li>What’s 5k x 50k? That’s 250 * thousand * thousand or 250 million.</li>
</ul>



<p><strong>2^6 = 64 (the sixes match: six and sixty-four)</strong><br>
<strong>2^10 ~ thousand</strong><br>
<strong>2^20 ~ million</strong><br>
<strong>2^30 ~ billion</strong></p>


<ul>
<li>Sure, 2 to the tenth = 1024, but 1000 is good enough for government work.</li>
<li>Have 32-bit color? That’s 2 + 30 bits, aka 2^2 billion, or 4 billion. </li>
<li>Have a 16-bit number? That’s 6 + 10 bits, or 2^6 thousand, or 64 thousand.</li>
</ul>



<p><strong>a% of b = b% of a</strong></p>


<ul>
<li>It’s not immediately clear, but it’s true: a% of b = a/100 * b, which is the same as b% of a (b/100 * a).</li>
<li>What’s 16% of 25? The same as 25% of 16: 4</li>
<li>What’s 43% of 200? Same as 200% of 43: 86. </li>
</ul></div>
</div>

<div class="feed">
  <div class="title">15. <a href="http://feeds.feedburner.com/~r/Betterexplained/~3/82673255/" target="_blank">The Rule of 72</a></div>
  <div class="content"><p> The Rule of 72 is a great <a href="http://betterexplained.com/articles/mental-math-shortcuts/" title="Mental Math Shortcuts">mental math shortcut</a> to estimate the effect of any growth rate, from quick financial calculations to population estimates. Here’s the formula:</p>

<blockquote><p>Years to double = 72 / Interest Rate</p></blockquote>

<p>This formula is useful for <strong>financial estimates</strong> and understanding the nature of compound interest. Examples:</p>


<ul>
<li>At 6% interest, your money takes 72/6 or 12 years to double.</li>
<li>To double your money in 10 years, get an interest rate of 72/10 or 7.2%.</li>
<li>If your country’s <span>GDP </span>grows at 3% a year, the economy doubles in 72/3 or 24 years. </li>
<li>If your growth slips to 2%, it will double in 36 years. If growth increases to 4%, the economy doubles in 18 years. Given the speed at which technology develops, shaving years off your growth time could be very important.</li>
</ul>



<p>You can also use the rule of 72 for <strong>expenses like inflation or interest</strong>:</p>


<ul>
<li>If inflation rates go from 2% to 3%, your money will lose half its value in 36 or 24 years. </li>
<li>If college tuition increases at 5% per year (which is faster than inflation), tuition costs will double in 72/5 or about 14.4 years. If you pay 15% interest on your credit cards, the amount you owe will <strong>double</strong> in only 72/15 or 4.8 years!</li>
</ul>



<p>The rule of 72 shows why a “small” 1% difference in inflation or <span>GDP </span>expansion has a huge effect in forecasting models.</p>

<p>By the way, the Rule of 72 applies to anything that grows, including population. Can you see why a population growth rate of 3% vs 2% could be a huge problem for planning? Instead of needing to double your capacity in 36 years, you only have 24. Twelve years were shaved off your schedule with one percentage point.</p>

<h2>Deriving the formula</h2>

<p>Half the fun in using this magic formula is seeing how it’s made. Our goal is to figure out how long it takes for some money (or something else) to double at a certain interest rate.</p>

<p>Let’s start with $1 since it’s easy to work with (the exact value doesn’t matter). So, suppose we have $1 and a yearly interest rate R. After one year we have:</p>

<p><code>
1 * (1+R)
</code></p>

<p>For example, at 10% interest, we’d have $1.10 at the end of the year. After 2 years, we’d have</p>

<p><code>
1 * (1+R) * (1+R)
= 1 * (1+R)^2
</code></p>

<p>And at 10% interest, we have $1.21 at the end of year 2. Notice how the dime we earned the first year starts earning money on its own (a penny). Next year we create another dime that starts making pennies for us, along with the small amount the first penny contributes. As Ben Franklin said: “The money that money earns, earns money”, or “The dime the dollar earned, earns a penny.” Cool, huh?</p>

<p>This deceptively small, cumulative growth makes compound interest extremely powerful - Einstein called it one of the most powerful forces in the universe.</p>

<p>Extending this year after year, after N years we have</p>

<p><code>
1 * (1+R)^N
</code></p>

<p>Now, we need to find how long it takes to double — that is, get to 2 dollars. The equation becomes:</p>

<p><code>
1 * (1+R)^N = 2
</code></p>

<p>Basically: How many years at R% interest does it take to get to 2? Not too hard, right? Let’s get to work on this sucka and find N:</p>



<pre>
1: 1 * (1+R)^N = 2
2: (1+R)^N = 2
3: ln( (1+R)^N ) = ln(2) [natural log of both sides]
4: N * ln(1+R) = .693
5: N * R = .693 [For small R, ln(1+R) ~ R]
6: N = .693 / R
</pre>



<p>There’s a little trickery on line 5. We use an approximation to say that ln(1+R) = R. It’s pretty close - even at R = .25 the approximation is 10% accurate (<a href="http://instacalc.com/?d=U2hvd2luZyB0aGUgZXJyb3IgYmV0d2VlbiBSIGFuZCBsbigxICsgUiku&amp;%23038;c=ciA9IC4yNXxlcnJvciA9IGxuKDEgKyByKSAtIHJ8YWNjdXJhY3kgPSBlcnJvciAvIHJ8fHx8&amp;%23038;s=sssssss&amp;%23038;v=0.8">check accuracy here</a>). As you use bigger rates, the accuracy will get worse.</p>

<p>Now let’s clean up the formula a bit. We want to use R as an integer (3) rather than a decimal (.03), so we multiply the right hand side by 100:</p>

<p><code>
N = 69.3 / R
</code></p>

<p>There’s one last step: 69.3 is nice and all, but not easily divisible. 72 is closeby, and has many more factors (2, 3, 4, 6, 12…). So the rule of 72 it is. Sorry 69.3, we hardly knew ye.</p>

<h2>Extra credit</h2>

<p>Derive a similar rule for tripling your money - just start with</p>

<p><code>
1 * (1+R)^N = 3
</code></p>

<p>Give it a go - if you get stuck, <a href="http://instacalc.com/?d=R2VuZXJhdGUgdGhlICJSdWxlIG9mIDcyIiBmb3IgYW55IGZhY3Rvciwgbm90IGp1c3QgZG91Ymxpbmcu&amp;%23038;c=ZmFjdG9yID0gM3wvLyBSdWxlIG9mLi4ufHJvdW5kdXAobG4oZmFjdG9yKSAqIDEwMCl8fHx8&amp;%23038;s=sssssss&amp;%23038;v=0.8">see the rule of 72 for any factor</a>. Happy math. </p></div>
</div>

<div class="feed">
  <div class="title">16. <a href="http://feeds.feedburner.com/~r/Betterexplained/~3/84065006/" target="_blank">Keeping your data backed up</a></div>
  <div class="content"><p> Backing up data is like flossing - we know we should, we just don’t get around to it as much as we’d like. Nobody will admit it, though <img src="http://betterexplained.com/wp-includes/images/smilies/icon_wink.gif" alt=";)"> .</p>

<p>Here are the rules of backup club:</p>


<ul>
<li><strong>Make it automatic</strong></li>
<li><strong>Make it easy</strong></li>
</ul>



<p>You might hear that the full-blown solution is to keep multiple versions of files around so if one got corrupted by a virus 3 months ago, you could restore to a version 3 months + 1 day old. Also, you need to make your backups in triplicate, sending each to geographically dispere locations along with one set of tapes constantly in the mail (re-mail it to yourself once you receive them).</p>

<p>Yeah, right. Just getting a redundant copy would be great for most people — don’t ask for the moon. Realistically, the best backup system is the one you actually use. So here’s the system I actually use.</p>

<h2>Backing Up Email</h2>

<p>Why would you backup email? Or Web-based email? <a href="http://googlesystem.blogspot.com/2006/03/gmail-horror-story-gmail-account.html">Lots</a> <a href="http://groups.google.com/group/Gmail-Past-discussions/browse_thread/thread/770063149423d0fb/f3989cb95b5ee2cb%23f3989cb95b5ee2cb">of</a> <a href="http://www.techcrunch.com/2006/12/28/gmail-disaster-reports-of-mass-email-deletions/">reasons</a>. To backup gmail, do the following:</p>


<ul>
<li><a href="https://mail.google.com/support/bin/answer.py?answer=10350&amp;%23038;query=pop3&amp;%23038;topic=&amp;%23038;type=f&amp;%23038;ctx=search">Enable <span>POP3 </span>for Gmail</a> and download into Outlook Express or Thunderbird. You can still keep your email on the server and read it from Gmail.</li>
<li><a href="https://mail.google.com/support/bin/answer.py?answer=10350&amp;%23038;query=pop3&amp;%23038;topic=&amp;%23038;type=f&amp;%23038;ctx=search">Export your contacts</a>. You might need to <a href="http://groups.google.com/group/Gmail-Problem-solving/browse_frm/thread/b89debfb14267c67">use Outlook Express as a middleman</a> in order to import into Thunderbird. Crazy, I know.</li>
</ul>



<p>Other providers: Hopefully they provide <span>POP </span>or <span>IMAP </span>access so you can download messages as above. Otherwise, consider forwarding your important messages. It’s a pain, I know, so be thankful if your email provider supports <span>POP </span>or <span>IMAP.</span></p>

<p>How’d we do?</p>


<ul>
<li>Automatic: Pretty much. Mail is downloaded will “automatically” be downloaded every time you open Thunderbird or Outlook Express. I’ve started using Thunderbird more and more — I still enjoy the desktop mail experience.</li>
<li>Easy: It’s middle-of-the-road. The instructions on gmail are a bit long, but only need to be done once. Ask your resident geek for help setting up your accounts.</li>
</ul>



<h2>Backing Up Files</h2>

<p><a href="https://www.foldershare.com/">FolderShare</a> is an easy way to keep directories in sync. You can even access files online if one of your computers is up.</p>

<p>I have my photos &amp; personal notes sync’d between a laptop and desktop. If either is online, they start syncing.</p>

<p>Have a friend? (I know you do, somewhere). Agree to have a common foldershare folder with a subdirectory for each of you. Each person drops their non-confidential files (or encrypted confidential files) in the backup folder. Everyone backs each other’s stuff up. Haven’t tried this yet, due to the aforementioned friend requirement.</p>

<p><a href="http://www.2brightsparks.com/syncback/syncback-hub.html">Syncback</a> is a simple tool that can copy one folder to another, on a schedule. I use this to copy my personal notes to my <span>USB </span>drive (always plugged in) on a schedule. Foldershare wouldn’t work because the <span>USB </span>drive is not actually a computer.</p>

<p>These are both automatic (foldershare more so - it transfers in the background), and fairly easy to setup. Again, this is only a one-time setup.</p>

<h2>Backing up Programs</h2>

<p>I haven’t found a great way to backup programs and settings. Luckily they are the most replacable of the bunch. Here’s what I use:</p>


<ul>
<li><strong>Portable applications:</strong> I have a directory (c:\tools) with a bunch of portable applications. You’d be surprised how many apps just work without a proper install. I have a subset of these on a <span>USB </span>drive.</li>
<li>Sometimes <strong>I’ll take screenshots</strong> of my start menu to remind myself what I have installed. For most apps, if you move to a machine and can’t remember an app, you probably didn’t use it that much.</li>
<li>I <strong>keep a directory for all the various installers</strong> (c:\download). I periodically back this up to CD (last “backup” was sometime last year) so I can re-install a new machine quickly. Realistically, I don’t mind losing the installers since I want the latest version anyway.</li>
<li>You can visit <a href="http://www.oldversion.com">oldversion.com</a> to get older (aka faster, simpler, less annoying) versions of software like Acrobat Reader or <span>AOL</span> Instant Messenger.</li>
</ul>



<p>These techniques aren’t automatic but are fairly easy.</p>

<h2>Backing Up Code and Websites You’ve Made</h2>

<p>Now these suckers aren’t replaceable, and if they change often (they do), you really want to use some type of version control. <a href="http://subversion.tigris.org/">Subversion</a> with the nice <a href="http://tortoisesvn.tigris.org/">TortoiseSVN</a> interface works for me.</p>

<p>I use subversion to backup and update the code, text and images on <a href="http://instacalc.com">InstaCalc.com</a>. I have a few other works-in-progress with their own repos.</p>

<p>However, I don’t backup the backup repository. Is it a theoretically problem? Yes. Does it bother me? Not too much - it’s on my list of upgrades.</p>

<p>Using source control is the most difficult backup strategy, but gives you the most power. Trust me, for the amount of time you spent on your code, it’s worth it.</p>

<h2>Backing Up Blogs</h2>

<p>Ah, blogging - I’m pretty new to this. Wordpress has a database backup plugin which, for some reason, isn’t working for me. So I’m going to dump the raw sql periodically:</p>



<pre>
<code>mysqldump --all-databases -u UserName -pPassword | gzip &gt; db.gz</code>
</pre>



<p>You can also email yourself a file periodically using <span>PHP.</span> I’ve put both tasks into a script that runs as a cron job, so I get a database backup every day.</p>

<p>Note there is <strong>no space</strong> between -p and the password. You can turn this into a shell script and have a cron job to email it to yourself every night.</p>

<p>You might also want to consider version-controlling your theme in case it changes. I haven’t done this yet, so this again falls in the theoretical-fixes category. Again, not too hard to remedy, but you need to watch out for setting files and caches that shouldn’t be saved.</p>

<p>Depending on the plugins you have, this can be easy and automatic. For a blog containing your memories, again, it’s worth it.</p>

<h2>Conclusion</h2>

<p>It’s tough to start a backup program before you really need it (it’s a bit like exercise that way). But try, try, try, and use the easy techniques above. It brings you peace of mind. Take even a small step each week, like backing up your web-based email. </p></div>
</div>

<div class="feed">
  <div class="title">17. <a href="http://feeds.feedburner.com/~r/Betterexplained/~3/87134772/" target="_blank">Understanding Quake’s Fast Inverse Square Root</a></div>
  <div class="content"><p> An <a href="http://www.beyond3d.com/articles/fastinvsqrt/">article</a> and <a href="http://www.lomont.org/Math/Papers/2003/InvSqrt.pdf">research paper</a> recently described an astonishingly fast, seemingly magical way to compute the inverse square root (1/sqrt(x)) in the game Quake.</p>

<p>I’m no expert in 3D graphics, but I can see how square roots are useful. The Pythagorean theorem computes distances, and can help normalize vectors (normalizing, by the way, is often just a fancy term for division). Games like Quake probably perform these calculations zillions (yes, zillions) of times a second, so speeding up this critical path is important for performance. And by the way, exponentiation and division are really, really expensive for the <span>CPU.</span></p>

<p>Given these conditions, here’s the function used to compute 1/sqrt(x) inside Quake:</p>



<pre>
<code>
float InvSqrt(float x){
   float xhalf = 0.5f * x;
   int i = *(int*)&amp;x; // store floating-point bits in integer
   i = 0x5f3759d5 - (i &gt;&gt; 1); // initial guess for Newton&#39;s method
   x = *(float*)&amp;i; // convert new bits into float
   x = x*(1.5f - xhalf*x*x); // One round of Newton&#39;s method
   return x;
}
</code>
</pre>



<p>Somehow, this code computes 1/sqrt(x) via multiplication and bit-shift operations. No division or exponents are required! Wow!</p>

<p><strong>Summary as I understand it:</strong> This is a great hack that picks an awesome initial value for Newton’s approximation method. The function takes the inverse root by negating and halving the exponent of a floating-point number written in scientific notation. It uses a additional transformations to correct for errors and tada, you have your inverse square root.</p>

<h2>Newton’s Method of Approximation</h2>

<p>Newton’s method can be used to find approximate roots of any function. You can keep iterating the method to get closer and closer to the root, but this function only uses 1 step! Here’s a crash-course on Newton’s method (it was new to me):</p>

<p>Let’s say you have a function f(x) and you want to find its root. Let’s call your original guess “g”. Newton’s method gives you a way to get a new, better guess:</p>

<p><img src="http://betterexplained.com/latexrender/pictures/3ab4b9465d2291176c02064ba5595bf3.gif" title="\displaystyle{new guess = g - \frac{f(g)}{f&amp;#039;(g)}}" alt="\displaystyle{new guess = g - \frac{f(g)}{f&amp;#039;(g)}}"></p>

<p>You can keep repeating this process (plugging in your new guess into the formula) and get closer approximations for your root. Eventually you have a “gnew” that makes f(gnew) really, really close to zero — it’s a root! (Or close enough for government work, as they say).</p>

<p>In our case, we want the inverse square function. Let’s say we have a number “i” (that’s all we start with, right?) and want to find the inverse square root: 1/sqrt(i). If we make a guess “x” as the inverse root, the error between our original number and our guess “x” is:</p>

<p><img src="http://betterexplained.com/latexrender/pictures/2fd54c246cd851b399d77ea95d0a744c.gif" title="\displaystyle{error(x) = \frac{1}{x^2} - i}" alt="\displaystyle{error(x) = \frac{1}{x^2} - i}"></p>

<p>This is because x is roughly 1/sqrt(i). If we square x we get “1/i”, and if we take the inverse we should get something close to “i”. If we subtract these two values, we can find our error.</p>

<p>Clearly, we want to make our error as small as possible. That means finding the “x” that makes error(x) = 0, which is the same as finding the root of the error equation. If we plug error(x) into Newton’s approximation formula:</p>

<p><img src="http://betterexplained.com/latexrender/pictures/846502f30bf9bfe8cb36ef8f68e61635.gif" title="\displaystyle{newguess = g - \frac{error(g)}{error&amp;#039;(g)}}" alt="\displaystyle{newguess = g - \frac{error(g)}{error&amp;#039;(g)}}"></p>

<p>and take the proper derivatives:</p>

<p><img src="http://betterexplained.com/latexrender/pictures/a41dfdff1865ef08c5d1383ced560c55.gif" title="\displaystyle{error(g)= g^{-2} - i}" alt="\displaystyle{error(g)= g^{-2} - i}"></p>

<p><img src="http://betterexplained.com/latexrender/pictures/ef7f926d32b4954ec4155b890b47340b.gif" title="\displaystyle{error&amp;#039;(g)= -2g^{-3}}" alt="\displaystyle{error&amp;#039;(g)= -2g^{-3}}"></p>

<p>we can plug them in to get the formula for a better guess:</p>

<p><img src="http://betterexplained.com/latexrender/pictures/643f1abc1d8b1edba6f72413148f28fe.gif" title="\displaystyle{newguess = g - \frac{g^{-2} - i}{-2g^{-3}} }" alt="\displaystyle{newguess = g - \frac{g^{-2} - i}{-2g^{-3}} }"></p>

<p><img src="http://betterexplained.com/latexrender/pictures/45a0acfb795838b56fd7c6d95c1b24d9.gif" title="\displaystyle{newguess = g - (-0.5g + 0.5ig^3) }" alt="\displaystyle{newguess = g - (-0.5g + 0.5ig^3) }"></p>

<p><img src="http://betterexplained.com/latexrender/pictures/21e7d1baf2136c75fe25732c3f81b560.gif" title="\displaystyle{newguess = 1.5g - 0.5ig^3}" alt="\displaystyle{newguess = 1.5g - 0.5ig^3}"></p>

<p><img src="http://betterexplained.com/latexrender/pictures/76592c839378ece27e05bc5b3bb105ee.gif" title="\displaystyle{newguess = g (1.5 - 0.5ig^2)}" alt="\displaystyle{newguess = g (1.5 - 0.5ig^2)}"></p>

<p>Which is exactly the equation you see in the code above, remembering that x is our new guess (g) and “xhalf” is half of the original value (0.5 * i):</p>

<p><code>x = x*(1.5f - xhalf*x*x);</code></p>

<p>With this formula, we can start with a guess “g” and repeat the formula to get better guesses. <a href="http://tinyurl.com/vh7hg">Try this demo</a> of using multiple iterations to find the inverse square. If you plug in different initial guesses (.2, .4, .8) you can see how quickly the guesses converge to the real answer.</p>

<p>So my friends, the question becomes: “How can we make a good initial guess?”</p>

<h2>Making a Good Guess</h2>

<p>So, what’s a good guess for the inverse square root? It’s a bit of a trick question — our best guess for the inverse square root is the inverse square root itself!</p>

<p>Ok hotshot, you ask, how do we <em>actually get</em> 1/sqrt(x)?</p>

<p>This is where the magic kicks in. Let’s say you have a number in exponent form or scientific notation:</p>

<p>10^6 = 1 million</p>

<p>Now, if you want to find the regular square root, you’d just divide the exponent by 2: <br>
<img src="http://betterexplained.com/latexrender/pictures/423d702277f2534399efae0a207c8fcd.gif" title="\displaystyle{\sqrt{10^6} = 10^{\frac{6}{2}} = 10^3 = 1,000}" alt="\displaystyle{\sqrt{10^6} = 10^{\frac{6}{2}} = 10^3 = 1,000}"></p>

<p>And if you want the <strong>inverse</strong> square root, divide the exponent by -2 to flip the sign: </p>

<p><img src="http://betterexplained.com/latexrender/pictures/20bd4b5194f209946e624978dcf5f6be.gif" title="\displaystyle{\frac{1}{\sqrt{10^6}} = 10^{\frac{6}{-2}} = 10^{-3} = \frac{1}{1,000}}" alt="\displaystyle{\frac{1}{\sqrt{10^6}} = 10^{\frac{6}{-2}} = 10^{-3} = \frac{1}{1,000}}"></p>

<p>So, how can we get the exponent of a number without other expensive operations?</p>

<h2>Floats are stored in mantissa-exponent form</h2>

<p>Well, we’re in luck. Floating-point numbers are stored by computers in mantissa-exponent form, so it’s possible to extract and divide the exponent!</p>

<p>But instead of explicitly doing division (expensive for the <span>CPU</span>), the code uses another clever hack: it shifts bits. Right-shifting by one position is the same as dividing by two (<a href="http://instacalc.com/?d=&amp;%23038;c=eCA9IDZ8biA9IDEwXnh8MS9zcXJ0KG4pfDEwXih4Ly0yKSAvLyBhbHNvIHRoZSBpbnZlcnNlIHNxdWFyZSByb290fHx8&amp;%23038;s=sssssss&amp;%23038;v=0.9">you can try this for any power of 2</a>, but it will truncate the remainder). And if you want to get a negative number, instead of multiplying by -1 (multiplications are expensive), just subtract the number from “0″ (subtractions are cheap).</p>

<p>So, the code converts the floating-point number into an integer. It then shifts the bits by one, which means the exponent bits are divided by 2 (when we eventually turn the bits back into a float). And lastly, to negate the exponent, we subtract from the magic number 0×5f3759d5. This does a few things: it preservers the mantissa (the non-exponent part, aka 5 in 5*10^6), handles odd-even exponents, shifting bits from the exponent <em>into</em> the mantissa, and all sorts of funky stuff. The paper has more details and explanation, I didn’t catch all of it the first time around. As always, feel free to comment if you have a better explanation of what’s happening.</p>

<p>The result is that we get an initial guess that is really close to the real inverse square root! We can then do a single round of Newton’s method to refine the guess. More rounds are possible (at an additional computational expense), but one round is all that’s needed for the precision needed.</p>

<h2>So, why the magic number?</h2>

<p>The great hack is how integers and floating-point numbers are stored. Floating-point numbers like “5.4 * 10^6″ store their exponent in a separate range of bits than “5.4″. When you shift the entire number, you divide the exponent by 2, as well as dividing the number (5.4) by 2 as well. This is where the magic number comes in — it does some cool corrections for this division, that I don’t quite understand. However, there are several magic numbers that could be used — this one happens to minimize the error in the mantissa.</p>

<p>The magic number also corrects for even/odd exponents; <a href="http://www.lomont.org/Math/Papers/2003/InvSqrt.pdf">the paper</a> mentions you can also find other magic numbers to use.</p>

<h2>Resources</h2>

<p>There’s further discussion on reddit (user pb_zeppelin) and slashdot:</p>


<ul>
<li><a href="http://programming.reddit.com/info/t9zb/comments">http://programming.reddit.com/info/t9zb/comments</a></li>
<li><a href="http://games.slashdot.org/article.pl?sid=06%2F12%2F01%2F184205">http://games.slashdot.org/article.pl?sid=06/12/01/184205</a> and <a href="http://games.slashdot.org/comments.pl?sid=209372&amp;%23038;cid=17077640">my comment</a> </li>
</ul></div>
</div>

<div class="feed">
  <div class="title">18. <a href="http://feeds.feedburner.com/~r/Betterexplained/~3/88645707/" target="_blank">Logical fallacy: popularity is not quality</a></div>
  <div class="content"><p> Arguing that an idea or product has merit because it has popular support is one of the oldest argument tactics around. It’s also one of the most flawed. Does McDonald’s have the <strong>best</strong> hamburger because they sell billions? Is a Toyota <strong>better</strong> than a Ferrari because one is far more common than the other? Is Britney Spears a better musician than Mozart, who you never hear on the radio? The counter-examples continue ad infinitum and ad nauseum.</p>

<p>Why does this occur? My guess is that it’s a confusion between cause and effect. <strong>Quality is often, but not always, a cause of popularity.</strong> If two products are functionally identical, then the one with better quality should become more popular over time. The problem with this theoretical situation is, well, that it’s theoretical. Two products are never the same. There’s different marketing, branding, advertising, consumer perception of company, packaging, features, design and price. All of these factors, and a few that I’ve left out, contribute to what the consumer will buy.</p>

<p>Even professional journalists can make the mistake of arguing that popularity indicates some inherent quality. Take a look at <a href="http://www.forbes.com/technology/2007/01/29/vista-windows-reviews_tech-cz_dl_0129vistareview.html">this article in Forbes about Vista adoption</a>:</p>

<blockquote>The Mac, for all of Apple’s snazzy advertising, has less than 1.6% share of the PC installed base. Linux, for all of the millions of column inches devoted to its wondrous abilities (many of those lines by yours truly), holds about 2.5% share. (All figures are from Gartner Dataquest.)

No wonder nobody at Microsoft seems too upset about the bad reviews of Vista. First of all, it’s easy (and fashionable) to talk trash about Microsoft. <strong>And it’s all too easy to overlook Microsoft’s achievements, like making operating system software that works well enough and is priced well enough to attract 96% of the world’s PC users</strong>.<br>
</blockquote>

<p>The article appears to say that Windows XP spread because of the “choice” of customers. Quick question: do you think most people actually choose their operating system, or just get whatever came on their machine?</p>

<p>Did you <strong>choose</strong> the operating system on your phone? Your TV? Your <span>PDA</span>? For the non-geeks out there, no. You got whatever OS the manufacturer put on there (if you bought it) or whatever your IT department put on there (if your company bought it for you).</p>

<p>So, the real question becomes <strong>“Why did the manufacturer/IT department install that particular OS?”</strong>. There’s a long, sordid history on this, but there are a few obvious reasons:</p>


<ul>
<li>If the computer manufacturer is Apple, they will install an Apple OS which supports their company — their definition of best.</li>
<li>If your company already runs Windows, the next computer they give you is likely to run Windows. Your IT admin will give you whatever is cheapest to operate, maintain and train you on — their definition of best.</li>
<li>If it comes from a PC manufacturer like Dell, they will include whatever sells the most and has the most profit — their definition of “best”. Notice that I <strong>didn’t</strong> say security, features, reliability or user interface. They will focus on what sells the best, and the reason it sells doesn’t really matter (except, perhaps, to forecast what will sell the best in the future).</li>
</ul>



<p>The question of what operating system is “best” is too general and doesn’t really make sense. Give me specifics: Fastest. Most reliable. Best price/performance. Most easy-to-use. Most compatible. Gimme some details! </p>

<p>Don’t presume that qualities like market share or usage is  a direct reflection of end-user choice. Sure, <strong>products can become popular</strong> because they have the best quality. Or, they can become popular because they have a lot of advertising. Or they have no competitors. Or because their name is first in the alphabet. Or people have inertia and don’t want to switch. There’s a lot of reasons. It’s a fallacy to think that just because something happened/was elected/was decided, it was the “best” decision. Sometimes things just happen. <strong>Look at a product’s popularity and ask why</strong>.</p>

<p>In the case of PCs, popularity is a reflection of the choice by the providers (manufacturers) and the people making the purchasing decisions (IT departments). In most cases, users are just along for the ride. The vast, vast majority of operating system sales are made to manufacturers and corporations. Not many people buy Windows off the shelf at Best Buy.</p>

<p>Rewording the article to say “Windows is the best platform for users because it has the most market share, the most applications and is compatible with the most devices” makes sense. Claiming it is the best simply because it attracted a lot of people doesn’t.</p>

<p>Of course, this reasoning may not stop screaming fans from declaring their system the “best” because it is widely used. It will just prevent you from believing them. </p></div>
</div>

<div class="feed">
  <div class="title">19. <a href="http://feeds.feedburner.com/~r/Betterexplained/~3/90118297/" target="_blank">Easy Permutations and Combinations</a></div>
  <div class="content"><p> I’ve always confused “permutation” and “combination” — which one’s which?</p>

<p>Here’s an easy way to remember: <strong>permutation sounds complicated</strong>, doesn’t it? And it is. With permutations, every little detail matters. Alice, Bob and Charlie is different from Charlie, Bob and Alice (insert your friends’ names here).</p>

<p>Combinations on the other hand, are pretty easy going. The details don’t matter. Alice, Bob and Charlie is the same as Charlie, Bob and Alice.</p>

<h2>Permutations: The hairy details</h2>

<p>Let’s start with permutations, or <strong>all possible ways</strong> of doing something. We’re using the fancy-pants term “permutation”, so we’re going to care about every last detail, including the order of items. Let’s say we have 8 people:</p>



<pre>
<code>
1: Alice
2: Bob
3: Charlie
4: David
5: Eve
6: Frank
7: George
8: Horatio
</code>
</pre>



<p>How many ways can we pick a Gold, Silver, and Bronze medal for “Best friend in the world”?</p>

<p>We’re going to use permutations since the order we hand out these medals matter. Here’s how it breaks down:</p>


<ul>
<li>Gold medal: 8 choices: A B C D E F G H (Clever how I made the names match up with letters, eh?). Let’s say A wins the Gold.</li>
<li>Silver medal: 7 choices: B C D E F G H. Let’s say B wins the silver.</li>
<li>Bronze medal: 6 choices: C D E F G H. Let’s say… C wins the bronze.</li>
</ul>



<p>We picked certain people to win, but the details don’t matter: we had 8 choices at first, then 7, then 6. The total number of options was 8 * 7 * 6 = 336.</p>

<p>Let’s look at the details. We had to order 3 people out of 8. To do this, we started with all options (8) then took them away one at a time (7, then 6) until we ran out of medals.</p>

<p>We know the factorial is: <img src="http://betterexplained.com/latexrender/pictures/94fa4ce6079c721dad82effba8142a42.gif" title="\displaystyle{8! = 8 \cdot 7 \cdot 6 \cdot 5 \cdot 4 \cdot 3 \cdot 2 \cdot 1 }" alt="\displaystyle{8! = 8 \cdot 7 \cdot 6 \cdot 5 \cdot 4 \cdot 3 \cdot 2 \cdot 1 }"></p>

<p>Unfortunately, that does too much! We only want 8 * 7 * 6. How can we “stop” the factorial at 5?</p>

<p>This is where permutations get cool: notice how we want to get rid of 5*4*3*2*1. What’s another name for this? 5 factorial!</p>

<p>So, if we do 8!/5! we get:</p>

<p><img src="http://betterexplained.com/latexrender/pictures/61ecd7fe1d7f131623fd0dcc57f5da0e.gif" title="\displaystyle{\frac{8!}{5!} = \frac{8 \cdot 7 \cdot 6 \cdot 5 \cdot 4 \cdot 3 \cdot 2 \cdot 1}{5 \cdot 4 \cdot 3 \cdot 2 \cdot 1}  = 8 \cdot 7 \cdot 6}" alt="\displaystyle{\frac{8!}{5!} = \frac{8 \cdot 7 \cdot 6 \cdot 5 \cdot 4 \cdot 3 \cdot 2 \cdot 1}{5 \cdot 4 \cdot 3 \cdot 2 \cdot 1}  = 8 \cdot 7 \cdot 6}"></p>

<p>And why did we use the number 5? Because it was left over after we picked 3 medals from 8. So, a better way to write this would be:</p>

<p><img src="http://betterexplained.com/latexrender/pictures/5b4d125e63d7498f8c3cb318454cf963.gif" title="\displaystyle{\frac{8!}{(8-3)!}}" alt="\displaystyle{\frac{8!}{(8-3)!}}"></p>

<p>where 8!/(8-3)! is just a fancy way of saying “Use the first 3 numbers of 8!”. If we have <strong>n</strong> items total and want to pick <strong>k</strong> in a certain order, we get:</p>

<p><img src="http://betterexplained.com/latexrender/pictures/899fa6e32e1e23123bda5aa00e21a119.gif" title="\displaystyle{\frac{n!}{(n-k)!}}" alt="\displaystyle{\frac{n!}{(n-k)!}}"> just means “Use the first k numbers of n!”</p>

<p>And this is the fancy permutation formula: You have <strong>n</strong> items and want to find the number of ways <strong>k</strong> items can be ordered:</p>

<p><img src="http://betterexplained.com/latexrender/pictures/7164200acc9606cf3547c31ddc023cdd.gif" title="\displaystyle{P(n,k) = \frac{n!}{(n-k)!}}" alt="\displaystyle{P(n,k) = \frac{n!}{(n-k)!}}"></p>

<h2>Combinations, Ho!</h2>

<p>Combinations are easy going. Order doesn’t matter. You can mix it up and it looks the same. Let’s say I’m a cheapskate and can’t afford separate Gold, Silver and Bronze medals. In fact, I can only afford empty tin cans.</p>

<p>How many ways can I give 3 tin cans to 8 people?</p>

<p>Well, in this case, the order we pick people doesn’t matter. If I give a can to Alice, Bob and then Charlie, it’s the same as giving to Charlie, Alice and then Bob. Either way, they’re going to be equally disappointed.</p>

<p>This raises and interesting point — we’ve got some redundancies here. Alice Bob Charlie = Charlie Bob Alice. For a moment, let’s just figure out how many ways we can rearrange 3 people.</p>

<p>Well, we have 3 choices for the first person, 2 for the second, and only 1 for the last. So we have 3 * 2 * 1 ways to re-arrange 3 people.</p>

<p>Wait a minute… this is looking a bit like a permutation! You tricked me!</p>

<p>Indeed I did. If you have N people and you want to know how many arrangements there are for <strong>all</strong> of them, it’s just N factorial or N!</p>

<p>So, if we have 3 tin cans to give away, there are 3! or 6 variations for every choice we pick. If we want to figure out how many combinations we have, we just <strong>create all the permutations and divide by all the redundancies</strong>. In our case, we get 336 permutations (from above), and we divide by the 6 redundancies for each permutation and get 336/6 = 56.</p>

<p>The general formula is</p>

<p><img src="http://betterexplained.com/latexrender/pictures/677d1e00b27408c0417be10dae451184.gif" title="\displaystyle{C(n,k) = \frac{P(n,k)}{k!}}" alt="\displaystyle{C(n,k) = \frac{P(n,k)}{k!}}"></p>

<p>which means “Find all the ways to pick k people from n, and divide by the k! variants”. Writing this out, we get our <strong>combination formula</strong>, or the number of ways to combine k items from a set of n:</p>

<p><img src="http://betterexplained.com/latexrender/pictures/be21eae92ceda7a78ac8b0042e7b7407.gif" title="\displaystyle{C(n,k) = \frac{n!}{(n-k)!k!}}" alt="\displaystyle{C(n,k) = \frac{n!}{(n-k)!k!}}"></p>

<h2>A few examples</h2>

<p>Here’s a few examples of combinations (order doesn’t matter) from permutations (order matters).</p>


<ul>
<li>Combination: Picking a team of 3 people from a group.<br>
Permutation: Picking a President, VP and Waterboy from a group.</li>
<li>Combination: Choosing 3 desserts from a menu of 10<br>
Permutation: Listing your 3 favorite desserts, in order, from a menu of 10</li>
</ul>



<p>Don’t memorize the formulas - it’s better to know why they work. <strong>Combinations sounds simpler than permutations, and they are. You have fewer combinations than permutations.</strong> </p></div>
</div>

<div class="feed">
  <div class="title">20. <a href="http://feeds.feedburner.com/~r/Betterexplained/~3/90419017/" target="_blank">Techniques for adding the numbers 1 to 100</a></div>
  <div class="content"><p> There’s a popular story that Gauss, mathematician extrodanaire, had a lazy teacher. The “educator” wanted to keep the kids busy so he could take a nap — he asked the class to add the numbers 1 to 100 - no small feat when most kids are still learning how to carry.</p>

<p>Gauss walked up and showed the answer on his blackboard: 5050. The teacher thought he had cheated, but no, Gauss had figured it out by sidestepping the problem. Manual addition is for suckers, Gauss had a better way:</p>

<p><strong>Sum from 1 to n =</strong> <img src="http://betterexplained.com/latexrender/pictures/511cae3c78ef34a0399f18f280881496.gif" title="\displaystyle{\frac{n(n+1)}{2}}" alt="\displaystyle{\frac{n(n+1)}{2}}"></p>

<p><strong>Sum from 1 to 100 =</strong> <img src="http://betterexplained.com/latexrender/pictures/937a59f7ddf80c21c1c0fce16a2125b9.gif" title="\displaystyle{\frac{100(100+1)}{2} = (50)(101) = 5050}" alt="\displaystyle{\frac{100(100+1)}{2} = (50)(101) = 5050}"></p>

<p>I’d like to share a few explanations for this formula, and help you really understand it intuitively.</p>

<h2>Technique 1: Pair Numbers</h2>

<p>A common approach people take is pairing numbers. Rather than using 1 to 100 (a bit tedious), let’s add the numbers 1 to 10. But instead of writing them all in a row, let’s wrap the numbers around, like this:</p>



<pre>
<code>
1  2  3  4  5
10 9  8  7  6
</code>
</pre>



<p>An interesting pattern emerges — <strong>the sum of each column is 11</strong>. As the top row increases, the bottom row decreases, so the sum stays the same.</p>

<p>Because 1 is paired with 10 (our n), we can say that each column has (n+1). And how many pairs do we have? Well, we have 2 equal rows, we must have n/2 pairs.</p>

<p>Number of pairs * Sum of each pair = <img src="http://betterexplained.com/latexrender/pictures/a3304e7b4fc4ae248a8c68442a107873.gif" title="\displaystyle{(\frac{n}{2})(n+1) = \frac{n(n+1)}{2}}" alt="\displaystyle{(\frac{n}{2})(n+1) = \frac{n(n+1)}{2}}"></p>

<p>which is the formula above.</p>

<h2>Wait — what about an odd number of items?</h2>

<p>Ah, I’m glad you brought it up. What if we are adding up the numbers 1 to 9? We don’t have an even number of items to pair up. Many explanations will just give the explanation above and leave it at that. I won’t.</p>

<p>Let’s add the numbers 1 to 9, but instead of starting from 1, let’s count from 0 instead:</p>



<pre>
<code>
0  1  2  3  4
9  8  7  6  5 
</code>
</pre>



<p>By counting from 0, we get an “extra item” (10 in total) so we can have an even number of rows. However, our formula will look a bit different. </p>

<p>Notice that each column has a sum of n (not n+1, like before), since 0 and 9 are grouped. And instead of having n items in 2 columns (for n/2 pairs total), we have n + 1 items in 2 columns (for (n + 1)/2 pairs total). If you plug these numbers in</p>

<p>Number of pairs * sum of each pair = <img src="http://betterexplained.com/latexrender/pictures/1d5a25e5b759992126a86b24d5e1aa71.gif" title="\displaystyle{(\frac{n + 1}{2})(n) = \frac{n(n+1)}{2}}" alt="\displaystyle{(\frac{n + 1}{2})(n) = \frac{n(n+1)}{2}}"></p>

<p>which is the same formula as before. It always bugged me that the same formula worked for both odd and even numbers - won’t you get a fraction? Yep, you get the same formula, but for different reasons.</p>

<h2>Technique 2: Use Two Rows</h2>

<p>The above method works, but you handle odd and even numbers differently. Isn’t there a better way? Yes.</p>

<p>Instead of looping the numbers around, let’s write them in two rows:</p>



<pre>
<code>
1  2  3  4  5  6  7  8  9  10
10 9  8  7  6  5  4  3  2  1
</code>
</pre>



<p>Notice that we have 10 pairs, and each pair adds up to 10+1.</p>

<p>The total of all the numbers above is</p>

<p>Total = pairs * size of each pair = <img src="http://betterexplained.com/latexrender/pictures/f4fb721b3df20bea95abd0d4d49544bd.gif" title="\displaystyle{n(n + 1)}" alt="\displaystyle{n(n + 1)}"></p>

<p>But we only want the sum of one row, not both. So we divide the formula above by 2 and get:</p>

<p><img src="http://betterexplained.com/latexrender/pictures/7bfee7e1a768105bec3d718ce6d9be5f.gif" title="\displaystyle{\frac{n(n + 1)}{2}}" alt="\displaystyle{\frac{n(n + 1)}{2}}"></p>

<p>Now this is cool (as cool as rows of numbers can be). It works for an odd or even number of items the same!</p>

<h2>Technique 3: Make a Rectangle</h2>

<p>I recently stumbled upon another explanation, a fresh approach to the old pairing explanation. Different explanations work better for different people, and I tend to like this one better.</p>

<p>Instead of writing out numbers, pretend we have beans. We want to add 1 bean to 2 beans to 3 beans… all the way up to 5 beans.</p>



<pre>
<code>
x
x x
x x x
x x x x
x x x x x
</code>
</pre>



<p>Sure, we could go to 10 or 100 beans, but with 5 you get the idea. How do we count the number of beans in our pyramid?</p>

<p>Well, the sum is clearly 1 + 2 + 3 + 4 + 5. But let’s look at it a different way. Let’s say we mirror our pyramid (I’ll use “o” for the mirrored beans), and then topple it over:</p>



<pre>
<code>
x                 o      x o o o o o
x x             o o      x x o o o o
x x x         o o o  =&gt;  x x x o o o
x x x x     o o o o      x x x x o o
x x x x x o o o o o      x x x x x o
</code>
</pre>



<p>Cool, huh? In case you’re wondering whether it “really” lines up, it does. Take a look at the bottom row of the regular pyramid, with 5′x (and 1 o). The next row of the pyramid has 1 less x (4 total) and 1 more o (2 total) to fill the gap. Just like the pairing, one side is increasing, and the other is decreasing.</p>

<p>Now for the explanation: How many beans do we have total? Well, that’s just the area of the rectangle.</p>

<p>We have n rows (we didn’t change the number of rows in the pyramid), and our collection is (n + 1) units wide, since 1 “o” is paired up with all the “x”s.</p>

<p><img src="http://betterexplained.com/latexrender/pictures/fef705f32272ea5fa177b8212dc8e579.gif" title="\displaystyle{Area = height \cdot width = n(n+1)}" alt="\displaystyle{Area = height \cdot width = n(n+1)}"></p>

<p>Notice that this time, we don’t care about n being odd or even - the total area formula works out just fine. If n is odd, we’ll have an even number of items (n+1) in each row.</p>

<p>But of course, we don’t want the total area (the number of x’s and o’s), we just want the number of x’s. Since we doubled the x’s to get the o’s, the x’s by themselves are just half of the total area:</p>

<p>Number of x’s = <img src="http://betterexplained.com/latexrender/pictures/806ddc25b9aea74513a18d3c5477f882.gif" title="\displaystyle{\frac{Area}{2} = \frac{n(n + 1)}{2}}" alt="\displaystyle{\frac{Area}{2} = \frac{n(n + 1)}{2}}"></p>

<p>And we’re back to our original formula. Again, the number of x’s in the pyramid = 1 + 2 + 3 + 4 + 5, or the sum from 1 to n.</p>

<h2>Technique 4: Average it out</h2>

<p>We all know that</p>

<p><code>
average = sum / number of items</code></p>

<p>which we can rewrite to</p>

<p><code>
sum = average * number of items</code></p>

<p>So let’s figure out the sum. If we have 100 numbers (1…100), then we clearly have 100 items. That was easy.</p>

<p>To get the average, notice that the numbers are all equally distributed. For every big number, there’s a small number on the other end. Let’s look at a small set:</p>



<pre>
<code>
1 2 3</code>
</pre>



<p>The average is 2. 2 is already in the middle, and 1 and 3 “cancel out” so their average is 2.</p>

<p>For an even number of items</p>



<pre>
<code>
1 2 3 4</code>
</pre>



<p>the average is between 2 and 3 - it’s 2.5. Even though we have a fractional average, this is ok — since we have an <strong>even</strong> number of items, when we multiply the average by the count that ugly fraction will disappear.</p>

<p>Notice in both cases, 1 is on one side of the average and N is equally far away on the other. So, we can say the average of the entire set is actually just the average of 1 and n: (1 + n)/2.</p>

<p>Putting this into our formula</p>

<p><img src="http://betterexplained.com/latexrender/pictures/50f9e87b05e0da94e78c75ca197106e9.gif" title="\displaystyle{sum = average * count = \frac{(1 + n)}{2} \cdot n = \frac{n(n + 1)}{2}}" alt="\displaystyle{sum = average * count = \frac{(1 + n)}{2} \cdot n = \frac{n(n + 1)}{2}}"></p>

<p>And voila! We have a fourth way of thinking about our formula.</p>

<h2>So why is this useful?</h2>

<p>Three reasons:</p>

<p>1. Adding up numbers quickly can be useful for estimation. Notice that the formula expands to this:</p>

<p><img src="http://betterexplained.com/latexrender/pictures/50a6a49c8e40eebf7ba08b5289aa4d56.gif" title="\displaystyle{\frac{n(n+1)}{2} = \frac{n^2}{2} + \frac{n}{2} }" alt="\displaystyle{\frac{n(n+1)}{2} = \frac{n^2}{2} + \frac{n}{2} }"></p>



<p>Let’s say you want to add the numbers from 1 to 1000: suppose you get 1 additional visitor to your site each day - how many total visitors will you have after 1000 days? Since <a href="http://betterexplained.com/articles/mental-math-shortcuts/">thousand squared = 1 million</a>, we get</p>

<p>million / 2 + 1000/2 = 500,500.</p>

<p>2. This concept of adding numbers 1 to N shows up in other places, like the binomial coefficients when multiplying polynomials. Having a firm grasp of this formula will help your math understanding in many areas.</p>

<p>3. Most importantly, this example shows there are many ways to understand a formula. Maybe you like the pairing method, maybe you prefer the rectangle technique, or maybe there’s another explanation that works for you. <strong>Don’t give up</strong> when you don’t understand — try to find another explanation that works. Happy math.</p>

<p>By the way, <a href="http://www.americanscientist.org/template/AssetDetail/assetid/50686?&amp;%23038;print=yes%2350975">there are more details</a> about the history of this story and the technique Gauss may have used. </p></div>
</div>

<div class="feed">
  <div class="title">21. <a href="http://feeds.feedburner.com/~r/Betterexplained/~3/91999622/" target="_blank">Marketing Lessons from InstaCalc.com</a></div>
  <div class="content"><p> I’m no expert, but creating your own project invariably teaches you a few things about presenting your site. Here are a few lessons that helped me launch <a href="http://instacalc.com">InstaCalc</a>, a real-time, shareable calculator.</p>

<h2>Get feedback, then release</h2>

<p>Don’t push out your initial version to the public right away. Create a version, iterate, create it again, iterate, and create it again. Show your friends, family, coworkers, neighbors… and then show the world.</p>

<p>I’m not a designer, but the site has improved markedly over the course of various iterations. Take a look at the progression:</p>

<p><a href="http://betterexplained.com/wp-content/uploads/instacalc/instacalc1.png" rel="lightbox"><img src="http://betterexplained.com/wp-content/uploads/instacalc/instacalc1_150.png" alt="instacalc1.png" title="instacalc1.png" border="0"></a></p>

<p><a href="http://betterexplained.com/wp-content/uploads/instacalc/instacalc2.png" rel="lightbox"><img src="http://betterexplained.com/wp-content/uploads/instacalc/instacalc2_150.png" alt="instacalc2.png" title="instacalc2.png" border="0"></a></p>

<p><a href="http://betterexplained.com/wp-content/uploads/instacalc/instacalc3.png" rel="lightbox"><img src="http://betterexplained.com/wp-content/uploads/instacalc/instacalc3_150.png" alt="instacalc3.png" title="instacalc3.png" border="0"></a></p>

<p>Of course, if you’re like me you can get caught in a constant trap of tweaking. It’s a learned skill, but have a sense of when it is “great” for a first-time visitor. It’s hard to look at your project with fresh eyes and imagine what a new visitor sees.</p>

<p>There’s a balance between refininement and “just get it done”, but it’s hard to impress first-time visitors without getting some feedback first. Don’t tell strangers about your site without first testing with your friends.</p>

<h2>Show, don’t tell</h2>

<p>Site visitors may not read your text, but they can’t avoid a screenshot. <strong>Show how your product is useful</strong>. My top features were being <strong>fast, easy and shareable</strong>. I could just have written text about this, and tried to explain it. After getting feedback (see above), I used quick text (only a line or two) and screenshots to show the capabilities:</p>

<p><img src="http://betterexplained.com/wp-content/uploads/instacalc/instacalc_features_homepage.png" alt="instacalc_features_homepage.png" title="instacalc_features_homepage.png" width="300" height="385" border="1px solid #ccc"></p>

<p>Is this the only way, the best way? No way. But I know it sends a clear, succinct message and is much more effective than a plain-text description. <a href="http://www.amazon.com/exec/obidos/ASIN/0789723107/instacalconli-20">Don’t make people think</a> about what your site does.</p>

<h2>Speak in terms of the known</h2>

<p>If you have a new idea, relate it to existing ideas. Rather than saying “This tool gets answers fast!”, which could be true but doesn’t tell us what it does, say: “Easier than Excel.”</p>

<p>This immediately:</p>


<ul>
<li>Gives a sense of the product: “Ok, it’s some sort of spreadsheet/calculation tool”</li>
<li>Uses all the existing Excel marketing. Everyone knows what a spreadsheet is because Microsoft and others have spent decades educating and selling them to people. Use the existing lingo.</li>
<li>Gives a concrete measure of comparison: Excel must be difficult to use in some way, let’s see how this stacks up.</li>
</ul>



<h2>Make it easy on the reviewers</h2>

<p>The reviewers are the ones who help spread the word. The points above make it easy for them: give people screenshots, taglines, and feature comparisons so reviewers have something to talk about.</p>

<p>As you can see, <a href="http://www.worldtechlogic.com/2007/01/09/instacalc-an-embeddable-simple-online-calculator/">multiple</a> <a href="http://www.lifehack.org/articles/lifehack/instacalc-a-powerful-online-calculator.html">sites</a> <a href="http://segatech.us/archives/1590">used</a> the text and screenshots as a starting point to discuss what was possible with their readers. Make your site reviewable. Have tours, <span>FAQ</span>s, easy screenshots and quoteable taglines.</p>

<h2>Put a name behind the site</h2>

<p>A <a href="http://billmonk.wordpress.com/">friend</a> suggested I put up a quick <a href="http://instacalc.com/blog/about">about page and bio</a>. This ended up being a big deal - there are lots of anonymous sites and services out there. Being able to say “InstaCalc was created by Kalid Azad, <a href="http://www.techcrunch.com/2006/11/01/instacalc-an-embedable-ajax-calculator-to-share/">a recent entrepreneur</a>…” in your review makes the article more substantial and adds credibility.</p>

<h2>Make it easy to get feedback</h2>

<p>I have three methods of feedback: an anonymous <a href="http://instacalc.com/blog/feedback">contact form</a>, <a href="http://instacalc.com/blog/tour">blog comments</a>, and an <a href="mailto:%6B%61%6C%69%64%40%69%6E%73%74%61%63%61%6C%63%2E%63%6F%6D">email address</a>. 95% of my feedback comes from the first two methods - most people can’t be bothered to send email directly, which is way more formal than a simple one-line comment in a blog or form.</p>

<p>Most people just want to fire off a quick note, so let them do it without a hassle. The contact form is fully anonymous - there are no required fields.</p>

<h2>Don’t take my word for it</h2>

<p>Like most things in life, experiment and see what works for you. Some products may be better served by secretive campaigns with less detail up front. Others might want anonymity (like the <a href="http://minimsft.blogspot.com">Mini-Microsoft blog</a>). Take these lessons as nuggets of information gleaned from a single example, and use them if they make sense. </p></div>
</div>

<div class="feed">
  <div class="title">22. <a href="http://feeds.feedburner.com/~r/Betterexplained/~3/92238038/" target="_blank">Vector Calculus: Understanding the Gradient</a></div>
  <div class="content"><p> The <strong>gradient</strong> is a fancy word for derivative, and it points in the direction of greatest increase of a function. The term gradient (or grad) typically refers to the derivative of vector functions, or functions of more than one variable. Even though a line has a gradient (the slope), using the term for single-variable functions is unnecessarily confusing.</p>

<p>As you may know, “gradient” also refers to gradual changes, such as one color blending smoothly into another. We’ll stick to the math definition here, but the meanings are related.</p>

<h2>Properties of the Gradient</h2>

<p>Now that we know the gradient is the derivative of a multi-variable function, let’s derive some properties.</p>

<p>The regular, plain-old derivative gives us the rate of change of a single variable, usually x (such as dF/dx, showing us how much the function F changes for a change in x). But a function that takes multiple variables, such as x and y, will have multiple derivatives: the function will change when we “wiggle” x (dF/dx) and when we wiggle y (dF/dy).</p>

<p>We can represent the multiple rates of change in a vector, with one component for each derivative. Thus, a function that takes 3 variables will have a gradient with 3 components:</p>

<p><img src="http://betterexplained.com/latexrender/pictures/94d6dfca196277bf7d0b144d5bfe139b.gif" title="\displaystyle{F(x)}" alt="\displaystyle{F(x)}"> has one variable and a single derivative: <img src="http://betterexplained.com/latexrender/pictures/716645b02d56bd11d78340bcf39da9f7.gif" title="\displaystyle{\frac{dF}{dx}}" alt="\displaystyle{\frac{dF}{dx}}"></p>

<p><img src="http://betterexplained.com/latexrender/pictures/d161070d2af20651ee0d8034849f4dcd.gif" title="\displaystyle{F(x,y,z)}" alt="\displaystyle{F(x,y,z)}"> has three variables and three derivatives: <img src="http://betterexplained.com/latexrender/pictures/13c8856e02bc4cf4cea67c6916bf34f9.gif" title="\displaystyle{(\frac{dF}{dx},\frac{dF}{dy},\frac{dF}{dz})}" alt="\displaystyle{(\frac{dF}{dx},\frac{dF}{dy},\frac{dF}{dz})}"></p>

<p>So, the gradient of a multi-variable function has a component for each direction.</p>

<p>Just like the regular derivative, the gradient points in the direction of greatest increase (I’ll create a derivation a bit later <img src="http://betterexplained.com/wp-includes/images/smilies/icon_smile.gif" alt=":)">  ). However, now that we have multiple directions to consider (x, y and z), the direction of greatest increase is no longer simply “forward” or “backward” along the x-axis, like it is with functions of a single variable. </p>

<p>If we have two variables, then our 2-component gradient can specify any direction on a plane. Likewise, with 3 variables, the gradient can specify and direction in space to move to increase our function.</p>

<h2>A Twisted Example</h2>

<p>I’m a big fan of examples to help solidify an explanation. Suppose we have a magical oven, with coordinates written on it and a special display screen:</p>

<p><img src="http://betterexplained.com/wp-content/uploads/gradient/gradient_microwave_1_1.jpg" alt="gradient_microwave_1_1.jpg" title="gradient_microwave_1_1.jpg" width="500" height="328" border="0"></p>

<p>We can put in any 3 coordinates (such as “3,5,2″) and the display shows us the <strong>gradient</strong> of the temperature at that point. The microwave also has a convenient clock. Unfortunately, the clock comes at a price — the temperature inside the microwave varies drastically from location to location. But this was worth it, we really wanted that clock.</p>

<p>Be careful not to confuse the coordinates and the gradient. The coordinates are the current location, measured on the x-y-z axis. The gradient is a <strong>direction to move from our current location</strong>, such as move up, down, left or right.</p>

<p>With me so far? We type in any coordinate, and the microwave spits out the gradient at that location.</p>

<p>Now suppose we are in need of psychiatric help and put the Pillsbury Dough Boy inside the oven because we think he would taste good. He’s made of cookie dough, right? We put him in some random point inside the oven, and our goal is to cook him as fast as possible. The gradient can help!</p>

<p>The gradient at any location points in the direction of <strong>greatest increase</strong> of a function: in this case, our function measures temperature. So, the gradient tells us which direction to move the doughboy to get him to cook even faster. Remember that the gradient does <strong>not</strong> give us the coordinates of where to go; it gives us the direction to move.</p>

<p>Thus, we would start out at any random point, like (3,5,2) and check the gradient. In this case, the gradient is (3,4,5). Now, we wouldn’t actually move an entire 3 units to the right, 4 units back, and 5 units up. The gradient is just a direction, so we’d follow this trajectory for a tiny bit, then check the gradient again.</p>

<p>This new point, pretty close to our original, would have its own gradient, the new best direction to follow. We’d keep repeating this process: move a tiny bit in the gradient direction, check the gradient, and a tiny bit in the new direction. Each time we followed the gradient we’d be moving to a hotter location.</p>

<p>Eventually, we’d come to the hottest part of the oven and we’d stay there, about to enjoy our fresh cookies.</p>

<h2>Don’t eat that cookie!</h2>

<p>But before you eat those cookies, let’s make some observations about the gradient. Isn’t that more fun?</p>

<p>First, when we reach the peak, the hottest temperature in the oven, what is the gradient at that location? </p>

<p>It will be zero. Why? Well, once you are at the maximum location, there is no direction of greatest increase: any direction you take will lead to a decrease in temperature, like being at the top of a mountain: any direction is downhill. Therefore, a zero gradient tells you to stay put - you are at the max of the function.</p>

<p>But wait… what if there are two maximums, like two mountains next to each other? You could be at the top of one mountain, but a bigger one is next to you. In order to get to the higher peak, you have to go downhill first.</p>

<p>Ah, now we are venturing into the not-so-pretty underbelly of the gradient. Finding the maximum in regular (1 variable) functions means we find all the places where the derivative is zero: there is no direction of greatest increase. If you recall, the regular derivative will point to <strong>local</strong> minimums and maximums, and the absolute max/min must be tested from these candidate locations.</p>

<p>The same applies for the gradient, which is just a generalization of the derivative. You must find multiple locations where the gradient is zero — you’ll have to test these points to see which one is the global maximum. Again, the top of each hill has a zero gradient — you need to compare the height at each to see which one is higher. Now that we have cleared that up, enjoy your cookie. </p>

<h2>Mathematics</h2>

<p>We know the definition of the gradient: a derivative for each variable of a function. The gradient symbol is usually an upside-down delta (a triangle), and called “del”. Taking our group of 3 derivatives above</p>

<p><img src="http://betterexplained.com/latexrender/pictures/b74a21aac14e8940dae48cffec6d15d1.gif" title="\displaystyle{grad F(x,y,z) = \nabla F(x,y,z) = (\frac{dF}{dx},\frac{dF}{dy},\frac{dF}{dz})}" alt="\displaystyle{grad F(x,y,z) = \nabla F(x,y,z) = (\frac{dF}{dx},\frac{dF}{dy},\frac{dF}{dz})}"></p>

<p>Notice how the x-component of the gradient is the partial derivative with respect to x (similar for y and z). For a one variable function, there is no y-component at all, so the gradient reduces to the derivative.</p>

<p>Also, notice how the gradient can itself be a function!</p>

<p><img src="http://betterexplained.com/latexrender/pictures/02fa0e1c23085f115a6ef75483f32fb0.gif" title="\displaystyle{F(x,y,z) = x + y^2 + z^3 }" alt="\displaystyle{F(x,y,z) = x + y^2 + z^3 }"></p>

<p><img src="http://betterexplained.com/latexrender/pictures/ebd3d984ce2301ff90e1acef8710ce35.gif" title="\displaystyle{\nabla F(x,y,z) = (1, 2y, 3z^2)}" alt="\displaystyle{\nabla F(x,y,z) = (1, 2y, 3z^2)}"></p>

<p>If we want to find the direction to move to increase our function the fastest, we plug in our current coordinates (such as 3,4,5) into the equation and get:</p>

<p><img src="http://betterexplained.com/latexrender/pictures/0aefe2f69a70ede6565aa035b0f611d7.gif" title="\displaystyle{direction = (1, 2(4), 3(5)^2) = (1, 8, 75)}" alt="\displaystyle{direction = (1, 2(4), 3(5)^2) = (1, 8, 75)}"></p>

<p>So, this new vector (1, 8, 75) would be the direction we’d move in to increase the value of our function. In this case, our x-component doesn’t add much to the value of the function: the partial derivative is always 1.</p>

<p>Obvious applications of the gradient are finding the max/min of multivariable functions. Another less obvious but related application is finding the maximum of a constrained function: a function whose x and y values have to lie in a certain domain, i.e. find the maximum of all points constrained to lie along a circle. Solving this calls for my boy Lagrange, but all in due time, all in due time: enjoy the gradient for now. </p>

<p>The key insight is to recognize the gradient as the generalization of the derivative. <strong>The gradient points to the maximum of the function; follow the gradient, and you will reach the local maximum.</strong> </p></div>
</div>

<div class="feed">
  <div class="title">23. <a href="http://feeds.feedburner.com/~r/Betterexplained/~3/93046841/" target="_blank">Vector Calculus: Understanding Circulation and Curl</a></div>
  <div class="content"><p> <strong>Circulation</strong> is the amount of force that pushes along a closed boundary or path. It’s the total  “push” you get when going along a path, such as a circle.</p>

<p>A vector field is usually the source of the circulation. If you had a paper boat in a whirlpool, the circulation would be the amount of force that pushed it along as it went in a circle. The more circulation, the more pushing force you have.</p>

<p><strong>Curl</strong> is simply the circulation per unit area, circulation density, or rate of rotation (amount of twisting at a single point). Imagine shrinking your whirlpool down smaller and smaller while keeping the force the same: you’ll have a lot of power in a small area, so will have a large curl. If you widen the whirlpool while keeping the force the same as before, then you’ll have a smaller curl. And of course, zero circulation means zero curl.</p>

<h2>Intuition</h2>

<p>Circulation is the amount of “pushing” force along a path. Curl is the amount of pushing, twisting, or turning force when you shrink the path down to a single point. Let’s use water as an example.</p>

<p>Suppose we have a flow of water and we want to determine if it has curl or not: is there any twisting or pushing force? To test this, we put a paddle wheel into the water and notice if it turns. If it does turn, it means this field has curl at that point. If it doesn’t turn, then there’s no curl.</p>

<p>What does it really mean if the paddle turns? Well, it means the water is pushing harder on one side than the other, making it twist. The larger the difference, the more forceful the twist and the bigger the curl. Also, a turning paddle wheel indicates that the field is “uneven” and not symmetric; if the field were even, then it would push on all sides equally and the paddle wouldn’t turn at all.</p>

<p>The fact that there is a “twist” means the field is <strong>not conservative</strong> (this has nothing to do with its political views).</p>

<p>A conservative field is “fair” in the sense that work needed to move from point A to point B, along any path, is the same. For example, consider a river: it’s field is conservative. Sure, you can get a free ride downstream, but then you have to do work to get back to your starting point. Or, you can do work to move upstream, and get a free ride back. Either way, the amount of work you “put in” is the same as what you get back.</p>

<p>However, in a field with curl (like a whirlpool), you can get a free ride by moving in the direction of the twist. In a whirlpool, you can get a free trip by moving with the current in a circle. If you fight the current and go the wrong way, you have to use energy with no free ride at all.</p>

<p>Conservative fields have zero curl: there are no free twists to push you along. Alternatively, if a field has curl, it is not conservative.</p>

<p>Gravity is another example of a conservative field. Technically, if you lift a rock and then let it fall, the energy you get from falling is the same as what you put in to lift the rock. Theoretically speaking, no energy was gained or lost in this transaction.</p>

<h2>Additional Details</h2>

<p>To be technical, curl is a <strong>vector</strong>, which means it has a both a magnitude and a direction.  The magnitude is simply the amount of twisting force at a point. </p>

<p>The direction is a little more tricky: it’s the orientation of the axis of your paddlewheel in order to get maximum rotation. In other words, it is the direction which will give you the most “free work” from the field. Imagine putting your paddlewheel sideways in the whirlpool - it wouldn’t turn at all. If you put it in the proper direction, it begins turning.</p>

<p>But wait a minute — aren’t there two directions to get a twisting motion? Couldn’t you just turn the paddlewheel “upside down” and get the maximum curl as well? </p>

<p>Yep, you’re right. By convention alone, if the paddle wheel is rotating counterclockwise, its curl vector points out of the page. This is a type of right-hand rule: make a fist with your right hand and stick out your thumb. If the circulation/pushing force follows the twisting of your fingers (counterclockwise), then the curl vector will be in the direction of your thumb.</p>

<h2>Mathematics</h2>

<p>Circulation is the integral of a vector field along a path - you are adding how much the field “pushes” you along a path. </p>

<p>How do we find this? Well, we should expect some type of dot product, because we want to know the amount that one vector (the force) is pushing in the direction of another (the path). So, the two vectors we need are (1) the path vector and (2) the field vector at every point along the path.</p>

<p>If we have a function that defines the position at any time, F(t), we can take the time derivative to get the velocity at that position.</p>

<p>The velocity vector is always in the direction of movement — if you are moving from A to B, the velocity vector will be an arrow from A to B, i.e. your change in position or your direction of movement. So, we can use the velocity to get our direction.</p>

<p>It’s important to understand why we aren’t using the position vector itself — it tells us where we are, but not where we’re going. We need to know our direction to see how much “push” we are getting: Knowing your position in a river isn’t important — are you going upstream or downstream, and at what angle?</p>

<p>The force vector (2) is defined by the field we are in. No derivatives or other changes are necessary — every point in the field has some force acting on it.</p>

<p>So, our formula for circulation is:</p>

<p>Force at position r = <img src="http://betterexplained.com/latexrender/pictures/a108b2ca681bfc01beafb0b0623e839d.gif" title="\displaystyle{F(r)}" alt="\displaystyle{F(r)}"><br>
Direction at position r = <img src="http://betterexplained.com/latexrender/pictures/65b691d304075004e24c84af1ae34c3d.gif" title="\displaystyle{dr}" alt="\displaystyle{dr}"><br>
Total pushing force = <img src="http://betterexplained.com/latexrender/pictures/a17a480b99a06d8e7927246e4937dc4f.gif" title="\displaystyle{Circulation = \int F(r) \cdot dr }" alt="\displaystyle{Circulation = \int F(r) \cdot dr }"></p>

<p>Remember, velocity is simply the derivative of position <strong>r</strong>, so <strong>dr</strong> is a vector giving us our direction. We integrate along the entire path and use the dot product to see how much pushing force is applied. We then sum up these “pushes” to get the total circulation.</p>

<p>Since curl is the circulation per unit area, we can take the circulation for a small area (letting the area shrink to 0). However, since curl is a vector, we need to give it a direction — the direction is normal (perpendicular) to the surface with the vector field. The <strong>magnitude</strong> is the same as before: circulation/area.</p>

<p>Recall that by convention (a bunch of people agreeing), counterclockwise circulation will give a curl pointing out of the page. Using these facts, we can create the formula for curl:</p>

<p>Curl = <img src="http://betterexplained.com/latexrender/pictures/fc859212d29dfd8802c2cdc1211d86e7.gif" title="\displaystyle{\frac{circulation}{area} = \frac{\int F(r) \cdot dr}{\int S}}" alt="\displaystyle{\frac{circulation}{area} = \frac{\int F(r) \cdot dr}{\int S}}"></p>

<p>Where S is the surface we are considering; the direction of the curl is the normal to the surface.</p>

<p>You’ll see fancier equations for curl where the surface shrinks to zero (such as in <a href="http://en.wikipedia.org/wiki/Curl">wikipedia</a>), but recognize the basic intuition — curl is the circulation per unit area.</p>

<h2>Parting Thoughts</h2>

<p>You’ll often see curl of a field F written like this:</p>

<p><img src="http://betterexplained.com/latexrender/pictures/c31d88f02a7852f4386fb8ded5147868.gif" title="\displaystyle{Curl(F) = \nabla \times F}" alt="\displaystyle{Curl(F) = \nabla \times F}"></p>

<p>which is a cross-product of the <a href="http://betterexplained.com/articles/vector-calculus-understanding-the-gradient/">gradient</a> and the field F. This has to do with how curl is actually computed, which will be material for another article (and probably in your textbook already — see <a href="http://en.wikipedia.org/wiki/Curl">wikipedia</a> for details).</p>

<p>If I have been successful, you should understand intuitively what circulation and curl mean, and how we got the formulae above. They spring up naturally from our definition of circulation as “pushing force along a path” and curl as “pushing force/area”. </p>

<p>Math should be a tool for clearly stating what we already know. Understand the intuition and then tackle the complicated formulas. Happy math.</p>

<p><span>PS.</span> Have some fun and check out this video of a famous whirlpool. Imagine the circulation on this (go on, imagine):</p>

<p><embed src="http://www.youtube.com/v/dHol4ICeDoo" width="425" height="350" allowScriptAccess="never" wmode="transparent" type="application/x-shockwave-flash"></embed> </p></div>
</div>

<div class="feed">
  <div class="title">24. <a href="http://feeds.feedburner.com/~r/Betterexplained/~3/94152077/" target="_blank">Speed Up Your Javascript Load Time</a></div>
  <div class="content"><p> Javascript is becoming increasingly popular on websites, from loading dynamic data via <span>AJAX </span>to adding special effects to your page.</p>

<p>Unfortunately, these features come at a price: you must often rely on heavy Javascript libraries that can add dozens or even hundreds of kilobytes to your page.</p>

<p>Users hate waiting, so here are a few techniques you can use to trim down your sites.</p>

<p>(<a href="http://betterexplained.com/articles/optimized-javascript-example/">Check out part 2</a> for downloadable examples.)</p>

<h2>Find The Flab</h2>

<p>Like any optimization technique, it helps to measure and figure out what parts are taking the longest. You might find that your images and <span>HTML </span>outweigh your scripts. Here’s a few ways to investigate:</p>

<p>1. The <b>Firefox <a href="https://addons.mozilla.org/firefox/60/">web-developer toolbar</a></b> lets you see a breakdown of file sizes for a page (Right Click &gt; Web Developer &gt; Information &gt; View Document Size). Look at the breakdown and see what is eating the majority if your bandwidth, and which files:</p>

<p><img src="http://betterexplained.com/wp-content/uploads/web/yahoo-size_1.PNG" alt="yahoo-size_1.PNG" title="yahoo-size_1.PNG" width="450" height="209" border="0"></p>

<p>2. The <b><a href="https://addons.mozilla.org/firefox/1843/">Firebug Plugin</a></b> also shows a breakdown of files - just go to the “Net” tab. You can also filter by file type:</p>

<p><img src="http://betterexplained.com/wp-content/uploads/web/yahoo_firebug.png" alt="yahoo_firebug.png" title="yahoo_firebug.png" width="450" height="136" border="0"></p>

<p>3. <b><a href="http://www.octagate.com/service/SiteTimer/">OctaGate SiteTimer</a></b> gives a clean, online chart of how long each file takes to download:</p>

<p><img src="http://betterexplained.com/wp-content/uploads/web/yahoo_octagate.png" alt="yahoo_octagate.png" title="yahoo_octagate.png" width="306" height="170" border="0"></p>

<p>Disgusted by the bloat? Decided your javascript needs to go? Let’s do it.</p>

<h2>Compress Your Javascript</h2>

<p>First, you can try to make the javascript file smaller itself. There are lots of utilities to “crunch” your files by removing whitespace and comments.</p>

<p>You can do this, but these tools can be finnicky and may make unwanted changes if your code isn’t formatted properly. Here’s what you can do:</p>

<p>1. <strong>Run <span>JSL</span>int</strong> (<a href="http://www.jslint.com/">online</a> or <a href="http://www.jslint.com/rhino/index.html">downloadable version</a>) to analyze your code and make sure it is well-formatted.</p>

<p>2. <b>Use <a href="http://dojotoolkit.org/docs/compressor_system.html">Rhino</a> </b> to compress your javascript. There are some <a href="http://dean.edwards.name/packer/">online packers</a>, but Rhino actually analyzes your source code so it has a low chance of changing it as it compresses, and it is scriptable.</p>

<p>Install Rhino (it requires Java), then run it from the command-line:</p>



<pre>
<code>
java -jar custom_rhino.jar -c myfile.js &gt; myfile.js.packed 2&gt;&amp;1
</code>
</pre>



<p>This compresses myfile.js and spits it out into myfile.js.packed. Rhino will remove spaces, comments and shorten variable names where appropriate. The “2&gt;&amp;1″ part means “redirect standard error to the same location as the output”, so you’ll see any error messages inside the packed file itself (cool, eh? <a href="http://aplawrence.com/Words/2003_12_29.html">Learn more here.</a>).</p>

<p>Using Rhino, I pack the original javascript and deploy the packed version to my website.</p>

<h2>Debugging Compressed Javascript</h2>

<p>Debugging compressed Javascript can be really difficult. I suggest creating a “debug” version of your page that references the original files. Once you test it and get the page working, pack it, test the packed version, and then deploy.</p>

<p>If you have a unit testing framework like <a href="http://www.jsunit.net/">jsunit</a>, it shouldn’t be hard to test the packed version.</p>

<h2>Eliminating Tedium</h2>

<p>Because typing these commands over and over can be tedious, you’ll probably want to <strong>create a script to run the packing commands</strong>. This .bat file will compress every .js file and create .js.packed:</p>



<pre>
<code>
compress_js.bat:
for /F %%F in (&#39;dir /b *.js&#39;) do java -jar custom_rhino.jar -c %%F &gt; %%F.packed 2&gt;&amp;1
</code>
</pre>



<p>Of course, you can use a better language like perl or bash to make this suit your needs.</p>

<h2>Optimize Javascript Placement</h2>

<p><strong>Place your javascript at the end of your <span>HTML</span></strong> file if possible. Notice how Google analytics and other stat tracking software wants to be right before the closing <code>&lt;/body&gt;</code> tag. </p>

<p>This allows the majority of page content (like images, tables, text) to be loaded and rendered first. The user sees content loading, so the page looks responsive. At this point, the heavy javascripts can begin loading near the end.</p>

<p>I used to have all my javascript crammed into the <code>&lt;head&gt;</code> section, but this was unnecessary. Only core files that are absolutely needed in the beginning of the page load should be there. The rest, like cool menu effects, transitions, etc. can be loaded later. You want the page to appear responsive (i.e., something is loading) up front.</p>

<h2>Load Javascript On-Demand</h2>

<p>An <a href="http://ajaxpatterns.org/On-Demand_Javascript"><span>AJAX </span>pattern</a> is to <strong>load javascript dynamically</strong>, or when the user runs a feature that requires your script. You can load an arbitrary javascript file from any domain using the following <a href="http://www.activewidgets.com/javascript.forum.6114.43/dynamic-load-javascript-from-javascript.html">import function</a>:</p>



<pre>
<code>
function $import(src){
  var scriptElem = document.createElement('script');
  scriptElem.setAttribute('src',src);
  scriptElem.setAttribute('type','text/javascript');
  document.getElementsByTagName('head')[0].appendChild(scriptElem);
}

// import with a random query parameter to avoid caching
function $importNoCache(src){
  var ms = new Date().getTime().toString();
  var seed = "?" + ms; 
  $import(src + seed);
}
</code>
</pre>



<p>The function <code>$import('http://example.com/myfile.js')</code> will add an element to the <code>head</code> of your document, just like including the file directly. The <code>$importNoCache</code> version adds a timestamp to the request to force your browser to get a new copy.</p>

<p>To test whether a file has fully loaded, you can do something like</p>



<pre>
<code>
if (myfunction){
  // loaded
}
else{ // not loaded yet
  $import('http://www.example.com/myfile.js');
}
</code>
</pre>



<p>There is an <a href="http://ajaxpatterns.org/On-Demand_Javascript#XMLHttpRequest-Based_On-Demand_Javascript"><span>AJAX </span>version as well</a> but I prefer this one because it is simpler and works for files in any domain.</p>

<h2>Delay Your Javascript</h2>

<p>Rather than loading your javascript on-demand (which can cause a gap), <strong>load your script  in the background, after a delay</strong>. Use something like</p>



<pre>
<code>
var delay = 5;
setTimeout("loadExtraFiles();", delay * 1000);
</code>
</pre>



<p>This will call <code>loadExtraFiles()</code> after 5 seconds, which should load the files you need (using <code>$import</code>). You can even have a function at the end of these imported files that does whatever initialization is needed (or calls an existing function to do the initialization).</p>

<p>The benefit of this is that you still get a fast initial page load, and users don’t have a pause when they want to use advanced features.</p>

<p>In the case of <a href="http://instacalc.com">InstaCalc</a>, there are heavy charting libraries that aren’t used that often. I’m currently testing a method to delay chart loading by a few seconds while the core functionality remains available from the beginning. You may need to refactor your code to deal with delayed loading of components. Some ideas are to use SetTimeout to poll the loading status periodically, or having a function called at the end of your included script to tell the main program the script has been loaded.</p>

<h2>Cache Your Files</h2>

<p>Another approach is to explicitly set the browser’s cache expiration. In order to do this, you’ll need access to <span>PHP </span>so you can send back certain headers.</p>

<p>Rename <code>myfile.js</code> to <code>myfile.js.php</code> and add <a href="http://wyome.com/docs/Compressing_Javascript_with_PHP">the following lines</a> to the top:</p>



<pre>
<code>
&lt;?php 
	header(&quot;Content-type: text/javascript; charset: UTF-8&quot;);
	header(&quot;Cache-Control: must-revalidate&quot;);
	$offset = 60 * 60 * 24 * 3;
	$ExpStr = &quot;Expires: &quot; . 
	gmdate(&quot;D, d M Y H:i:s&quot;,
	time() + $offset) . &quot; GMT&quot;;
	header($ExpStr);
?&gt;
</code>
</pre>



<p>In this case, the cache will expire in (60 * 60 * 24 * 3) seconds or <a href="http://instacalc.com/?d=&amp;%23038;c=MyBkYXlzIGluIHNlY29uZHN8NjAgKiA2MCAqIDI0ICogMyAvLyB0aGUgaGFyZCB3YXl8fHx8fA&amp;%23038;s=sssssss&amp;%23038;v=0.9">3 days</a>. Be careful with using this for your own files, especially if they are under development. I’d suggest caching library files that you won’t change often.</p>

<p>If you accidentally cache something for too long, you can use the <code>$importNoCache</code> trick to add a datestamp like “myfile.js?123456″ to your request (which is ignored). Because the filename is different, the browser will request a new version.</p>

<p>Setting the browser cache doesn’t speed up the initial download, but can help if your site references the same files on multiple pages, or for repeat visitors.</p>

<h2>Combine Your Files</h2>

<p>A great method I initially forgot is merging several javascript files into one. Your browser can only have so many connections to a website open at a time — given the overhead to set up each connection, it makes sense to combine several small scripts into a larger one.</p>

<p>But you don’t have to combine files manually! Use a script to merge the files — check out part 2 for an example script to do this. Giant files are difficult to edit - it’s nice to break your library into smaller components that can be combined later, just like you break up a C program into smaller modules.</p>

<h2>Should I Gzip It?</h2>

<p>You probably should. I originally said no, because some older browsers have problems with compressed content.</p>

<p>But the web is moving forward. Major sites like Google and Yahoo use it, and the problems in the older browsers aren’t widespread.</p>

<p>The benefits of compression, often a 75% or more reduction in file size, are too good to ignore: <a href="http://betterexplained.com/articles/how-to-optimize-your-site-with-gzip-compression/">optimize your site with <span>HTTP </span>compression.</a></p>

<h2>All done? Keep learning.</h2>

<p>Once you’ve performed the techniques above, recheck your page size using the tools above to see the before-and-after difference.</p>

<p>I’m not an expert on these methods — I’m learning as I go. Here are some additional references to dive in deeper:</p>


<ul>
<li><a href="http://ajaxpatterns.org/Patterns#Performance_Optimisation">Ajax patterns: Performance Optimization</a></li>
<li><a href="http://www.thinkvitamin.com/features/webapps/serving-javascript-fast">Think vitamin: Serving Javascript Fast</a></li>
<li><a href="http://www.die.net/musings/page_load_time/">Detailed post on page load time</a></li>
<li><a href="http://www.mnot.net/cache_docs/">Detailed Caching Tutorial</a> and <a href="http://www.ircache.net/cgi-bin/cacheability.py">online tool to check your cacheability</a></li>
</ul>



<p>Keep your scripts lean, and <a href="http://betterexplained.com/articles/optimized-javascript-example/">read part 2</a> for some working examples. </p></div>
</div>

<div class="feed">
  <div class="title">25. <a href="http://feeds.feedburner.com/~r/Betterexplained/~3/94863055/" target="_blank">A Simple, Comprehensive Overview of Javascript</a></div>
  <div class="content"><p> This isn’t a <a href="http://www.w3schools.com/js/default.asp">reference guide</a>, <a href="http://www.yourhtmlsource.com/javascript/basicjavascript.html">programming tutorial</a> or <a href="http://simon.incutio.com/slides/2006/etech/javascript/js-reintroduction-notes.html">detailed overview</a>. It’s a Javascript refresher, assuming you know another programming language and puts all the information in one place (just ctrl+f to find!). Javascript <strong>has nothing to do with Java</strong>, is <strong>dynamically typed</strong> and has <strong>C-like syntax</strong>.</p>

<h2>Enabling Javascript</h2>



<pre>
<code>
Include javascript inside HTML:
&lt;script&gt;
  x = 3;
&lt;/script&gt;

Reference external file:
&lt;script src=&quot;http://example.com/script.js&quot;&gt;&lt;/script&gt;

Redirect if javascript disabled:
&lt;noscript&gt;&lt;meta http-equiv=&quot;refresh&quot; content=&quot;0; URL=http://example.com/noscript.html&quot;/&gt;&lt;/noscript&gt;
</code>
</pre>



<h2>Using Variables, Objects and Arrays</h2>



<pre>
<code>
var str = "Hello";  	          // local variable
str2 = "Hello World"; 	          /* global */
str3 = 'My quote char: " ';       // single or double quote
str4 = "My really really really \
really long string broken into \
multiple lines";

str = 19;                         // change to int
str = 0xfe + 2.343 + 2.5e3;       // hex, floats, exp

var newObject = new Object();	  // constructor
newObject = {};			  // shorthand for same
newObject.name = "bob"            // dynamic attributes 
newObject.name = null		  // it's there (null item)
delete newObject.name		  // it's gone (undefined)
newObject["real age"] = 33;       // array notation/hash table

var obj = {			  // create object using JSON
    name: "Bob",		  //   aka Javascript Object Notation
    details: {
        age: 33,
        "favorite color": "green"
    }
}
obj.name
obj.details["favorite color"]

var newArray = [];                // no size 
newArray[3] = "hi";               // grows dynamically
newArray[2] = 13;                 // any type
newArray.push(newObject);         // add new item
newArray.pop();		          // remove it
</code>
</pre>



<h2>Comparisons and Manipulations</h2>

<p>Javascript has some funky types and comparisons.</p>



<pre>
<code>
/* javascript types */
typeof(&quot;string&quot;) == &quot;string&quot;
typeof(3) == typeof(3.4) == typeof(0x34) == &quot;number&quot;
typeof(myObject) = typeof(myArray) == &quot;object&quot; // arrays are objects
typeof(true) == typeof(1 == 2) == &quot;boolean&quot;
typeof(Math.sin) == &quot;function&quot;
typeof(notthere) == &quot;undefined&quot;

/* comparisons */
123 == &quot;123&quot;                     // true =&gt; converts type
123 === &quot;123&quot;                    // false =&gt; checks type
typeof(x) == &quot;undefined&quot;	 // x isn&#39;t there
x == null			 // x is defined, but null

/* Numbers */
parseInt(&quot;123&quot;)			 // base 10 =&gt; 123
parseInt(&quot;123&quot;, 16);		 // base 16 =&gt; 291
parseFloat(&quot;123.43&quot;);		 // 123.43

isNaN(0/0) == true		 // illegal number
3/0 == Infinity			 // legal...
-3/0 == -Infinity		 // 
isFinite(3/0) == false		 // ... but not finite

/* regular expression (regex) string comparisons */
matches = &quot;hello&quot;.match(/h../)   // returns array [&quot;hel&quot;] or null

re = new RegExp(&quot;h..&quot;, &quot;ig&quot;);	 // construct regexp -- no slashes
matches = &quot;hello&quot;.match(re);     // use it

&quot;hello&quot;.replace(/h/,&quot;b&quot;)	 // =&gt; &quot;bello&quot;
</code>
</pre>



<h2>Conditionals and Loops</h2>



<pre>
<code>
if (str == &quot;Hello&quot;){	// if-else
  alert(&quot;Hi&quot;);		// popup dialog
}
else{
  alert(&quot;something is wrong!&quot;);
}

a = 3, b = 4;		// multi-assigment
c = a &gt; b ? a : b;	// c gets bigger item (b)

switch (name){		// switch statement
  case &quot;Bob&quot;:
    alert(&quot;Hi Bob!&quot;)
    break
  case &quot;Joe&quot;:
    alert(&quot;Hey Joe.&quot;)
    break
  default: alert(&quot;Do I know you?&quot;)
}

while (i &lt; n){          // the basics
 // do something
 i++;
}

for (var i=0; i&lt;n; i++){
  // do something else
}
</code>
</pre>



<h2>Defining Functions</h2>



<pre>
<code>
function foo(a,b){          // global function
  return a + b;
}

var fn = function(a,b){     // save function as variable...
  return foo(a,b);
}

obj.fn = function(a,b){     // ...or as part of object
  return a + b;
}

function bar(a,b){
    var n = 1;		            // local var

    function helper(x) {            // inner function...
        return 1/Math.sqrt(x + n);  // .. can use local vars
    }
    return helper(input);           // avoid need for global function
}

foo(1,2) == fn(1,2) == 3;   // true
</code>
</pre>



<h2>Javascript Classes</h2>

<p>Javascript doesn’t have formal class notation, but you can create a “constructor” and add methods to it. Examples from <a href="http://simon.incutio.com/slides/2006/etech/javascript/js-reintroduction-notes.html">here</a>.</p>



<pre>
<code>
function Person(first, last) { // create &quot;constructor&quot;
    this.first = first;	       // public variables -- reference current object
    this.last = last;

    var privateFn = function(first, last){ 	// private function
	// ...
    }

    self.setName = function(first, last){ // public function
       	this.first = first;
	this.last = last;
    }

}

Person.prototype.fullName = function() { // extend prototype
    return this.first + &#39; &#39; + this.last; //   even at runtime!
}

var bob = new Person(&quot;Bob&quot;, &quot;Smith&quot;); // &quot;new&quot; creates an object
bob.fullName();			      // =&gt; &quot;Bob Smith&quot;
</code>
</pre>



<h2>Advanced Javascript</h2>



<pre>
<code>
function foo(a,b){        // will raise exception
  var c = a + b;
  throw "Error Message"; // your message here
}

try{                     // try dangerous code
   foo(1,2);
}
catch(e){			// catch exception
   alert(e.name + e.message);	// show details
}

eval("x = 3");		 // execute arbitrary code

timer = setTimeout("myfunction()", 1000)  // execute in 1 second (1000ms)
clearTimeout(timer);			  // cancel event
</code>
</pre>



<h2>Browser Document Object Model (DOM)</h2>

<p>Find and change <span>HTML </span>elements.</p>



<pre>
<code>
x = document.getElementById(&quot;foo&quot;);    // finds &lt;div id=&quot;foo&quot;&gt;&lt;/div&gt;

x.style.background = &quot;#333&quot;;	       // set CSS style
x.style.borderLeft = &quot;1px solid #ccc&quot;; // border-left =&gt; borderLeft (camelCase)

x.className = &quot;myclass&quot;;	       // set CSS class
x.innerHTML = &quot;Hello&quot;;		       // set html inside div

y = document.getElementById(&quot;myinput&quot;); // input area/textarea
y.value = &quot;Hi&quot;;			        // get or set text
</code>
</pre>



<h2>Keep learning</h2>

<p>I made this page as a list of important examples; read the advanced guides if any section is confusing. A later post will cover the helper functions I use in my applications. Good luck. </p></div>
</div>

<div class="feed">
  <div class="title">26. <a href="http://feeds.feedburner.com/~r/Betterexplained/~3/96124060/" target="_blank">Speed Up Your Javascript, Part 2: Downloadable Examples!</a></div>
  <div class="content"><p> I’m happy people are finding the <a href="http://betterexplained.com/articles/speed-up-your-javascript-load-time/">article on javascript optimization</a> useful. But I made a giant, horrible mistake. A mistake that befalls many tutorials.</p>

<p><strong>I didn’t include actual, working examples for you to play with.</strong> You can talk all you want, but until you’ve got some code, it’s just theory and listless sighs. And without seeing the code walk (or run! Get it?), it’s hard to believe that it really works. So here’s some live, working examples to show these techniques in action:</p>


<ul>
<li><strong>Online Example</strong>: <a href="http://betterexplained.com/examples/javascript-loading/sample.html">Imported and Delayed Loading of Javascript</a>. Notice how the delayed javascript file appears 5 seconds after the regular file and the <code>$import</code>ed one.</li>
<li><strong>Download</strong>: <a href="http://betterexplained.com/examples/javascript-loading/optimized_javascript.zip">Optimized_Javascript.zip</a></li>
</ul>



<p>The examples are free and in the public domain. However, if you find it useful I’d appreciate you sharing it with friends or <a href="http://betterexplained.com/contact/">dropping me a note</a>. I like knowing what explanation styles work so I can do more of it in the future.</p>

<p>And now, the guided tour of what you’ll see in the zip file.</p>

<h2>Eliminate Tedium: Use Scripts</h2>

<p>Automate, automate, automate! I’ve created a set of batch files (and .sh files for you Linux/UNIX gurus) to get you started:</p>


<ul>
<li><code>makeall.bat</code>: Runs the commands below</li>
<li><code>make_libraries.bat</code>: Combines *.js into “allfiles.lib.js”, and combines files prefixed with “example” into “example.lib.js”.</li>
<li><code>pack_js.bat</code>: Compresses *.js and creates *.js.packed</li>
<li><code>add_cache_header.bat</code>: Inserts the <span>PHP </span>caching header into the .js.packed files, creating js.packed.php</li>
<li><code>cleanup.bat</code>: Removes generated files, leaving you with your original .js files.</li>
</ul>



<p>These are templates - modify them to suit your own needs. If you find yourself typing a command again and again, throw it into a script.</p>

<h2>Compressing Javascript</h2>

<p>I’ve included <code>custom_rhino.jar</code> which does the compression (<a href="http://dojotoolkit.org/docs/compressor_system.html">more info</a>).</p>

<p>There are a few javascript files for demo purposes. The first is <code>example_compressed.js</code>, which has extremely long variable names in various scopes (local and global). Take a look at this sucker, it’s ripe to get crunched by Rhino:</p>



<pre>
<code>
/* these names are global, so will not get compressed (could be used elsewhere) */
var LongName = 1;
var OtherLongName = 2;
var ReallyReallyLongName = 3;
var AbsurdlyLongNameImNotQuiteSureWhyAnyoneWouldUseThisButItIsGoodForExamples = 3;

/* these names are local to foo(), and will get compressed. Isn't Rhino awesome? */
function foo(){

var LongName = 1;
var OtherLongName = 2;
var ReallyReallyLongName = 3;
var AbsurdlyLongNameImNotQuiteSureWhyAnyoneWouldUseThisButItIsGoodForExamples = 3;
var LongName = 1;
var OtherLongName = 2;
var ReallyReallyLongName = 3;
var AbsurdlyLongNameImNotQuiteSureWhyAnyoneWouldUseThisButItIsGoodForExamples = 3;
(repeated...)
return 0; // of course :)
}

log("Compressed Example Loaded!");
</code>
</pre>



<p>A typical “javascript compressor” will simply remove extra spaces and comments, which doesn’t help much. Rhino actually analyzes your code: when it sees global variables, it knows the name shouldn’t be changed since other scripts may reference them.</p>

<p>But local variables are another story. Since locals are only referenced <strong>inside of their function</strong>, they are ripe for squashing. This is your javascript on Rhino:</p>



<pre>
<code>
var LongName=1;
var OtherLongName=2;
var ReallyReallyLongName=3;
var AbsurdlyLongNameImNotQuiteSureWhyAnyoneWouldUseThisButItIsGoodForExamples = 3;

function foo(){
var _1=1;
var _2=2;
var _3=3;
var _4=3;
var _1=1;
var _2=2;
var _3=3;
var _4=3;
var _1=1;
var _2=2;
...
return 0;
}
log("Compressed Example Loaded!");
</code>
</pre>



<p>Any questions?</p>

<p>Rhino trampled the variable names and replaced them with the shortest identifiers it could find: _1, _2, etc. This saves a lot of space, and has the side-effect of partially obfuscating your code (if you are looking for that sort of thing).</p>

<h2>Dynamic Import and Delayed Loading</h2>

<p>Now here’s the fun stuff: <code>example_imported.js</code> and <code>example_delayed.js</code> don’t do anything special, except call a logging function that shows when they were loaded. </p>

<p>Check out <code>sample.html</code></p>



<pre>
<code>
&lt;html&gt;
&lt;head&gt;
&lt;!-- only put scripts here if you really need to --&gt;
&lt;/head&gt;
&lt;body&gt;

&lt;!-- Scripts that need to run first --&gt;
&lt;script src=&quot;import.js&quot;&gt;&lt;/script&gt;
&lt;script&gt;
/* Simple logger -- looks for div with id &quot;log&quot;. 
   We want this available from the get-go. */

function log(str){
  var logger = document.getElementById(&quot;log&quot;);
  if (logger){
	logger.innerHTML += new Date().toString() + &quot;: &quot;;
	logger.innerHTML += str;
	logger.innerHTML += &quot;&lt;br/&gt;&quot;;
  }
}
&lt;/script&gt;

&lt;!-- content, images, tables, etc. --&gt;
Put content here...

&lt;div id=&quot;log&quot;&gt;
&lt;/div&gt;

&lt;!-- include packed version --&gt; 
&lt;script src=&quot;example_compressed.js.packed&quot;&gt;&lt;/script&gt;

&lt;!-- dynamic import --&gt;
&lt;script&gt;
	$import(&#39;example_imported.js&#39;);
&lt;/script&gt;

&lt;!-- delayed loading --&gt;
&lt;script&gt;
	function loadDelayedScripts(){
		$import(&#39;example_delayed.js&#39;);
	}
	
	var delay = 5; // wait and then load the file
	setTimeout(&quot;loadDelayedScripts()&quot;, delay * 1000);
&lt;/script&gt;

&lt;!-- other heavy scripts, tracking code, etc. --&gt;
&lt;/body&gt;
&lt;/html&gt;
</code>
</pre>



<p>Take a look at <a href="http://betterexplained.com/examples/javascript-loading/sample.html">the result</a>:</p>

<p><img src="http://betterexplained.com/wp-content/uploads/javascript/sample_html.png" alt="sample_html.png" title="sample_html.png" width="500" height="85" border="0"></p>

<p>Notice how <code>$import</code> acts immediately, and the delayed load happens 5 seconds later. All the scripts call the <code>log</code> function, but it could be any callback, like <code>registerLoadEvent()</code> or <code>displayHiddenFeature()</code>. Leave that for your imagination.</p>

<h2>Creating Library Files</h2>

<p>It can also be helpful to combine smaller files into a larger one, especially if they don’t change often. This reduces the number of requests the browser makes and you don’t suffer the overhead for each item.</p>

<p>Downloading one 10k script is faster than ten 1k ones - browsers can only have a certain number of connections open at a time. Once you’ve got the connection going, you may as well cram a larger file down.</p>

<p>The <span>UNIX </span>“cat” (or Windows “type”) command is perfect for this. If you set a filter (example*.js) you can combine files with the same prefix into a library:</p>



<pre>
<code>
cat *.js &gt; allfiles.lib.js
cat example*.js &gt; example.lib.js
</code>
</pre>



<p>And since the library ends in .js, it will get packed along with the other .js files in our packing script.</p>

<h2>Adding <span>PHP</span> Cache Headers</h2>

<p>The last step is to add the cache headers to the files. There is a general “set_cache_header.php” file that is combined with the packed javascript (.js.packed) to create the js.packed.php files. Assuming your server is configured to serve <span>PHP, </span>this will set the caching headers for 3 days (change this to <a href="http://instacalc.com/?d=&amp;%23038;c=MyBkYXlzIGluIHNlY29uZHN8fHx8fHw&amp;%23038;s=sssssss&amp;%23038;v=0.9">any number you like</a>).</p>

<h2>Always Keep learning</h2>

<p>We’re never done learning — I’d love to see what other tricks you use to speed up your javascript or automate the “build” process.</p>

<p>Remember that there are all sorts of interesting callbacks you can do. The scripts, once loaded, can call functions to display previously hidden features in the page: as scripts are loaded, menu items/images/text could appear. Or, you can just have one master script that <code>$imports</code> the others, so you don’t need to monkey with your <span>HTML </span>file if you add a new javascript file (some Javascript libraries behave this way). The possibilities go on: use some, all, or none of these techniques. Experiment and learn what works for you.</p>

<p>Happy hacking. </p></div>
</div>

<div class="feed">
  <div class="title">27. <a href="http://feeds.feedburner.com/~r/Betterexplained/~3/98641923/" target="_blank">Combining Simplicity and Complexity</a></div>
  <div class="content"><p> There’s an <a href="http://www.jnd.org/dn.mss/simplicity_is_highly.html">ongoing</a> <a href="http://www.joelonsoftware.com/items/2006/12/09.html">debate</a> about the merits of simplicity vs. complexity. “People want things simple and easy to use!” proclaim some.</p>

<p>“Balderdash - they want complex and powerful behavior!” exclaims the other side. And back and forth they argue, gnashing teeth and brandishing keyboards.</p>

<p>I think the problem lies in the <strong>confusion of terminology</strong>, which the authors hint at but don’t state explicitly. Pitting simplicity against complexity in a virtual cage match creates a false dichotomy, or the belief that you must choose one or the other. Both are possible.</p>

<p>This isn’t a cop-out, “can’t we all just get along” answer. I think the real issue is that we are mixing terms. Simplicity and complexity really can be friends, and don’t have to fight to the death (like Kirk and Spock they are best friends, and even if they do fight it’s only a charade). </p>

<p>When we argue about a thing being simple or complex, we are unknowingly asking two higher questions:</p>


<ul>
<li>How easy is it to understand?</li>
<li>What can it do?</li>
</ul>



<p>These are the questions we ineffectively try to answer using the words “simple” and “complex”. Unfortunately two words aren’t enough; we need four to answers these two questions:</p>

<p><strong>How easy is it to understand?</strong></p>


<ul>
<li>Simple: Easy to understand, straightfoward</li>
<li>Complicated: Difficult to understand, convoluted</li>
</ul>



<p><strong>What can it do?</strong></p>


<ul>
<li>Advanced: Does a lot, powerful</li>
<li>Basic: Doesn’t do much, simplistic</li>
</ul>



<p>Framing the problem this way lets us separate out the good and bad answer for each question.</p>

<p>Being simple or complex is a good thing. Being simplistic is an ok thing. Being complicated is a bad thing. Let’s see why.</p>

<h2>Case 1: Simple and Basic</h2>

<p>Simple and basic is the <strong>stereotype of simple</strong>: We think that if something is easy to understand, it isn’t capable of much.</p>

<p>This is true a lot of the time. Think about a rock, an oar, or a spoon. These are basic tools and easy to use, though they don’t seem to accomplish much. In software world we have notepad: it’s easy to understand, but not very powerful (no spell checking, embedded graphics, etc.).</p>

<p><img src="http://betterexplained.com/wp-content/uploads/complexity/notepad.PNG" alt="notepad.PNG" title="notepad.PNG" width="293" height="158" border="0"></p>

<p>Are simple, simplistic things good? You bet. They get their (simple) job done. And their ease of understanding is a great benefit: it’s excellent for education purposes, and often times we don’t need the power we think we do. In fact, having simple behavior often leads to increased reliability — how often does an oar “break down” compared to an engine?</p>

<p>Also, there’s nothing stopping you from taking multiple “weak” items to create a powerful one, like using thin threads to make a thick rope. I’ll touch on this later.</p>

<p>Rocks, spoons, and notepad are fine in my book: they have their uses.</p>

<h2>Case 2: Complicated and Basic</h2>

<p>Ah, now this is a strange beast. What item could be hard to understand yet not accomplish much?</p>

<p><strong>A Rube Goldberg machine</strong>. It’s a contraption built for a basic task, such as lowering a sign, using absurdly convoluted and intricate means. Here’s what I mean:</p>

<p><embed src="http://www.youtube.com/v/UScbWzhieNc" width="425" height="350" allowScriptAccess="never" wmode="transparent" type="application/x-shockwave-flash"></embed></p>

<p>These devices are complicated (can you immediately tell what it will do?) and basic (lowering a sign isn’t very awe-inspiring). They stink from a practical viewpoint, though they do have redeeming value for entertainment, artistic or educational purposes (how <strong>not</strong> to build a device).</p>

<p>Unfortunately some software is like a Rube Goldberg machine, like setting the clock on your <span>VCR.</span> Setting the clock should be a simple task, but it often involves a complicated, unweidly procedure because the interface of a <span>VCR </span>is not designed for it (”Press channel up to pick the date…”).</p>

<p>Strangely enough, people don’t seem to get entertainment value from seeing how complicated this is. They get frustrated, which is a bad thing. Items in this category should be avoided.</p>

<h2>Case 3: Complicated and Advanced</h2>

<p>This is the stereotype of most powerful devices: Sure, they can do a lot, but they are really hard to use.</p>

<p>A real-life example is a helicopter. It can fly straight up, backwards, and manuever in any way imaginable: it’s extremely powerful. Unfortunately, you need extensive training in order to operate one. I’ve never flown in one, but apparently it requires the use of all 4 limbs and your brain in order to <a href="http://www.drf.de/cockpit_helicopter.html?&amp;%23038;L=1">operate it</a>.</p>

<p><img src="http://betterexplained.com/wp-content/uploads/complexity/helicopter.jpg" alt="helicopter.jpg" title="helicopter.jpg" width="361" height="250" border="0"></p>

<p>Items in this realm are often on the cutting-edge. They are our most advanced technology that works, but we are still in the process of figuring it out, discovering patterns and optimizations that make it easier to use.</p>

<p>Computers were originally complicated and advanced. Early computers were powerful (they could perform any computation, albeit slowly), but were very hard to use. You had to use punch cards or even enter information manually using switches. As time went on, we developed keyboards, graphical interfaces, and better programming languages. We were able to input instructions in a more simple (easy to understand) manner.</p>

<p>Programming is still not dead-simple: <strong>simplicity and ease of use is a range</strong>. But it’s clear that computers are vastly simpler (easier to use) than they used to be. The underlying technology, microchips, has become more advanced (powerful) and also more complicated (difficult to understand) in order to make computers simpler. There was a time when a single person could understand a microchip or operating system — no longer.</p>

<p>Complicated and advanced devices are “ok” — they do get the difficult jobs done, but when using them you often think there’s got to be a better way. And there often is.</p>

<h2>Case 4: Simple and Advanced</h2>

<p>Ah, this is the holy grail. This is what mathematicians seek by “elegant” equations, what scientists yearn for with beautiful theories, what designers seek when creating products. Consider Einstein’s famous equation:</p>

<p><img src="http://betterexplained.com/latexrender/pictures/49f78bfe611741b2dde74b2d6aea7777.gif" title="\displaystyle{E = mc^2}" alt="\displaystyle{E = mc^2}"></p>

<p>Energy and matter are equivalent - you can convert one into the other. This concept is astonishingly easy to understand (simple) yet describes extremely powerful behavior (nuclear reactions). Many physics equations are like this: Gravity, Newton’s laws or Maxwell’s equations. They are 1-liners that guide us through powerful behavior, the workings of our universe.</p>

<p>Google is a simple interface for incredibly powerful behavior — finding any document on the Web. The ipod (I’ve never owned one) is claimed to be an amazingly simple device to manage and play music. Even programming can be like this: A Turing machine is a simple model of computation (writing symbols on a ticker tape) that can do the same calculations we can perform on a modern computer. Heck, MacGuyver can create a bomb out of simple, basic parts like a toothpick, comb and bottle of shampoo.</p>

<h2>Use Simple Building Blocks</h2>

<p>Advanced behavior often comes from simple parts. The beauty of the Unix design philosophy was to have many simple, even basic programs that did a single task well: combining files, sorting them, counting lines or searching for words. Each tool was basic, but when linked via “pipes” could lead to very powerful behavior. If you are a programmer, I urge you to learn about Unix if you don’t already.</p>

<p>Much of the world is made from simple, easy-to-understand building blocks. Simple atoms make any object. Simple <span>DNA </span>(only 4 bases) is stretched into long sequences, creating the instructions needed to make a human being. The most intricate video file is still a sequence of 1’s and 0’s. </p>

<p>It didn’t have to be this way. We could have had thousands of elements in the periodic table. <span>DNA </span>could have had millions of different bases. We could have designed computers to store files with 0’s, 1’s and 2’s.</p>

<p>But that’s not what happened. The best-designed, most elegant systems are simple and advanced. Simplicity gives them reliability, and a clever arrangement of parts gives them power.</p>

<h2>Simple Isn’t Easy</h2>

<p>There’s one giant caveat here: “easy to understand” does <strong>not</strong> mean “easy to do”. Running a marathon is easy to understand. It is not easy to do. Similarly, actually <strong>creating</strong> powerful behavior from the easy-to-understand parts can be a challenge.</p>

<p>It’s tough to find underlying patterns in chaotic, complicated behaviors. It’s easy to get something working and leave it at that. But looking at our natural laws, there’s inspiration that nearly any complex phenomenon or design can be built simply.</p>

<h2>Parting Thoughts</h2>

<p>Have I resolved the debate? You be the judge. To me, simplicity and complexity coexist peacefully by thinking about two separate questions:</p>

<p>1) How easy is it to understand?<br>
2) What can it do?</p>

<p>Don’t confuse a simple interface with basic behavior. Don’t assume a complicated device is powerful. Think about these questions independently and you’ll be fine. </p></div>
</div>

<div class="feed">
  <div class="title">28. <a href="http://feeds.feedburner.com/~r/Betterexplained/~3/100192162/" target="_blank">Understanding the Pareto Principle (The 80/20 Rule)</a></div>
  <div class="content"><p> Originally, the Pareto Principle referred to the observation that 80% of Italy’s wealth belonged to only 20% of the population.</p>

<p>More generally, the Pareto Principle is the observation (not law) that <strong>most things in life are not distributed evenly</strong>. It can mean all of the following things:</p>


<ul>
<li>20% of the input creates 80% of the result</li>
<li>20% of the workers produce 80% of the result</li>
<li>20% of the customers create 80% of the revenue</li>
<li>20% of the bugs cause 80% of the crashes</li>
<li>20% of the features cause 80% of the usage</li>
<li>And on and on…</li>
</ul>



<p>But be careful when using this idea! First, there’s a common misconception that the numbers 20 and 80 must add to 100 — they don’t!</p>

<p>20% of the workers could create 10% of the result. Or 50%. Or 80%. Or 99%, or even 100%. Think about it — in a group of 100 workers, 20 could do all the work while the other 80 goof off. In that case, 20% of the workers did 100% of the work. Remember that the 80/20 rule is a rough guide about <strong>typical distributions</strong>.</p>

<p>Also recognize that the numbers don’t have to be “20%” and “80%” exactly. The key point is that <strong>most things in life (effort, reward, output) are not distributed evenly - some contribute more than others</strong>.</p>

<h2>Life Isn’t Fair</h2>

<p>What does it mean when we say “things aren’t distributed evenly”? The key point is that each unit of work (or time) doesn’t contribute the same amount.</p>

<p>In a perfect world, every employee would contribute the same amount, every bug would be equally important, every feature would be equally loved by users. Planning would be so easy.</p>

<p>But that isn’t always the case:</p>

<p><img src="http://betterexplained.com/wp-content/uploads/pareto/pareto_graph.png" alt="pareto_graph.png" title="pareto_graph.png" width="400" height="399" border="0"></p>

<p>The 80/20 rule observes that most things have an unequal distribution. Out of 5 things, perhaps 1 will be “cool”. That cool thing/idea/person will result in majority of the impact of the group (the green line). We’d like life to be like the red line, where every piece contributes equally, but that doesn’t always happen.</p>

<p>Of course, this ratio can change. It could be 80/20, 90/10, or 90/20 (remember, the numbers don’t have to add to 100!).</p>

<p>The key point is that most things are <strong>not</strong> 1/1, where each unit of “input” (effort, time, labor) contributes exactly the same amount of output.</p>

<h2>So Why Is This Useful?</h2>

<p>The Pareto Principle helps you realize that the majority of results come from a minority of inputs. Knowing this, if…</p>

<p>20% of workers contribute 80% of results: Focus on rewarding these employees.<br>
20% of bugs contribute 80% of crashes: Focus on fixing these bugs first.<br>
20% of customers contribute 80% of revenue: Focus on satisfying these customers.</p>

<p>The examples go on. The point is to realize that you can often focus your effort on the 20% that makes a difference, instead of the 80% that doesn’t add much.</p>

<p>In economics terms, there is <strong>diminishing marginal benefit</strong>. This is related to the law of diminishing returns: each additional hour of effort, each extra worker is adding less “oomph” to the final result. By the end, you are spending lots of time on the minor details.</p>

<h2>A Fun, Non-Math Example, Please</h2>

<p>Everything is nice and rosy in the abstract. I want to give you a real example. Take a look at this awesome video of an artist drawing a car in Microsoft Paint. It’s pretty phenomenal what can be accomplished with such a basic tool:</p>

<p><embed src="http://www.youtube.com/v/ElrldD02if0" width="425" height="350" allowScriptAccess="never" wmode="transparent" type="application/x-shockwave-flash"></embed></p>

<p>Now let’s deconstruct this video. It’s about 5 minutes long, so each minute is about 20% of the way to completion (of course the video is sped up, but we are only interested in relative times anyway). Take a look at how the car evolved over time:</p>

<p><img src="http://betterexplained.com/wp-content/uploads/pareto/car_1_06_100.jpg" alt="car_1_06_100.jpg" title="car_1_06_100.jpg" width="150" height="100" border="0">
<img src="http://betterexplained.com/wp-content/uploads/pareto/car_2_00_100.jpg" alt="car_2_00_100.jpg" title="car_2_00_100.jpg" width="151" height="100" border="0">
<img src="http://betterexplained.com/wp-content/uploads/pareto/car_3_05_100.jpg" alt="car_3_05_100.jpg" title="car_3_05_100.jpg" width="151" height="100" border="0">
<img src="http://betterexplained.com/wp-content/uploads/pareto/car_4_04_100.jpg" alt="car_4_04_100.jpg" title="car_4_04_100.jpg" width="151" height="100" border="0">
<img src="http://betterexplained.com/wp-content/uploads/pareto/car_5_05_100.jpg" alt="car_5_05_100.jpg" title="car_5_05_100.jpg" width="151" height="100" border="0"></p>

<p><strong>1:06 (Level 1)</strong> - Wireframe<br>
<strong>2:00 (Level 2)</strong> - Basic coloring<br>
<strong>3:05 (Level 3)</strong> - Beginning details: rims, windshield<br>
<strong>4:04 (Level 4)</strong> - Advanced details: shading, reflections<br>
<strong>5:05 (Level 5)</strong> - Finishing touches: headlights, background</p>

<p>Now, let’s say the artist was creating potential designs for a client. Given 5 minutes of time, he could present:</p>


<ul>
<li>A single car at top quality (Level 5)</li>
<li>A reasonably detailed car (Level 3) and a colorized wireframe (Level 2)</li>
<li>5 cars at a wireframe level (5 Level 1s)</li>
</ul>



<p>“But #5 is way better than #1!!!” someone will inevitably shout.</p>

<p>The point isn’t that #5 is better than #1 — it clearly is. The question is whether #5 is better than five #1s, or some other combination.</p>

<p>Let’s say your customer doesn’t know whether they want a car, a truck, or a boat, let alone the color. Spending the time to create a Level 5 drawing wouldn’t make sense — show some concepts, get a general direction, and then work out the details.</p>

<p>The point is to put in the amount of effort needed to get the most bang for your buck — it’s usually in the first 20% (or 10%, or 30% — the exact amount can vary). In the planning stage, it may be better to get 5 fast prototypes rather than 1 polished product.</p>

<p>In this example, after 1 minute (20% of the time) we have a great understanding of what the final outcome will be. Most of the “work” is done up front, in the sense of deciding the type of vehicle, body style, and perspective. The rest is “filling in details” like colors and shading. </p>

<p>This isn’t to say the details are easy — they’re not — but each detail does not add as much to the picture as the broad strokes in the beginning. The difference between #4 and #5 is not as great as #1 and #2, or better yet, a blank drawing and #1 (the time from 0:00 to 1:06). You really have to look to see the differences on the car between #4 and #5, while the contribution #1 makes is quite obvious.</p>

<h2>Concluding Thoughts</h2>

<p>This may not be the best strategy in every case. The point of the Pareto principle is to <strong>recognize that most things in life are not distributed evenly</strong>.  Make decisions on allocating time, resources and effort based on this:</p>


<ul>
<li>Instead of 1 hour on a rough draft for an article you may write, spend 10 minutes on 6 outlines for a paper / blog article and pick the best topic.</li>
<li>Instead of investing 3 hours on a website, spend 30 minutes and create 6 different template layouts.</li>
<li>Rather than spending 3 hours to read 3 articles in detail (which may not be relevant to you), spend 5 minutes glancing through 12 articles (1 hour) and then spend an hour each on the two best ones (2 hours).</li>
</ul>



<p>These techniques may or may not make sense - the point is to realize you have the option to focus on the important 20%.</p>

<p>Lastly, don’t think the Pareto Principle means only do 80% of the work needed. It may be true that 80% of a bridge is built in the first 20% of the time, but you still need the rest of the bridge in order for it to work. It may be true that 80% of the Mona Lisa was painted in the first 20% of the time, but it wouldn’t be the masterpiece it is without all the details. <strong>The Pareto Principle is an observation, not a law of nature.</strong> </p>

<p>When you are seeking top quality, you need all 100%. When you are trying to optimize your bang for the buck, focusing on the critical 20% is a time-saver. See what activities generate the most results and give them your appropriate attention. </p></div>
</div>

<div class="feed">
  <div class="title">29. <a href="http://feeds.feedburner.com/~r/Betterexplained/~3/101082045/" target="_blank">Surviving (and thriving) on your own: Know Thyself</a></div>
  <div class="content"><p> I’ve <a href="http://fortes.com/">been</a> <a href="http://blog.craigdossantos.com/">talking</a> <a href="http://trevinchow.com/blog/">with</a> <a href="http://ashhsie.com/">my</a> <a href="http://savrajsingh.com">friends</a> about the experience of leaving Microsoft to <a href="http://instacalc.com">start my own projects</a>. We all have different tips and thoughts, and I’d like to throw my own into the ring. This is the first part of a series on the topic.</p>

<p>Most articles about happiness and work give you abstract quotes about following your passions. Yes, those thoughts are useful (and I’ll share a few) but I’ll link to those — I want to share the <strong>specific details</strong> that have worked for me.</p>


<ul>
<li>Keep a “Feel Good” list</li>
<li>Keep an Idea List</li>
<li>Read the experts</li>
</ul>



<p>And most importantly: Don’t trick yourself.</p>

<h2>Starting off: So…what do you want to do?</h2>

<p>Ah, the tough question, the one that started this quest and what you may dread: Why do you want to go off on your own, anyway? What are you going to do? Isn’t the regular life good enough?</p>

<p>There are no easy answers. Even after months of reflection before and after <a href="http://instacalc.com">leaving Microsoft to try entrepreneurship</a>, the decision isn’t crystal clear. Only now, after over half a year, do I feel really comfortable with my choice.</p>

<p>Thinking about it more, I don’t <strong>want</strong> there to be a solid answer to “What should I do?”. Imagine being told your life’s work at age 12 (”Molecular Biologist”). Sure, that job may be great for you, but wouldn’t you at least wonder about the other options?</p>

<p>I prefer life to be a bit more of a surprise — I realize everyone is not like this. My stance is that it’s a good thing to get a bit lost, go through some tough times, inspiring you to search and <a href="http://www.stumbleupon.com/">stumble upon</a> the things you truly enjoy. It makes you appreciate your work. You need rain to treasure sunshine — and both are needed to make the beautiful, brilliant, one-in-a-million flower of your heart grow. You need to touch a hot stove to <strong>really know deep down inside</strong> that it isn’t for you. Take your pick of analogy, the “I’m a unique and special snowflake” stuff is not really my style.</p>

<h2>Don’t trick yourself</h2>

<p>Beware the traps of <strong>justification and attribution</strong> when thinking about your life. Many people justify past decisions because they want them to make sense and feel better.  “Yes, I like wandering around and then finding my passion because it makes me appreciate it more,” you might have heard someone say. For some, it seems that no matter what happens, it was the “right thing” or that it was “meant to be that way”. I’m on the fence about whether this is a coping mechanism, optimistic viewpoint (I’m an optimist), or whether it makes you appreciate life more.</p>

<p>Yes, most clouds have silver linings. But sometimes getting hit in the head with a shovel is <strong>just getting hit in the head with a shovel</strong>. It’s not fun, there’s not much educational value, and your life really is better without it.</p>

<p>Other people <strong>attribute every effect to a cause</strong>. They see an effect and start looking frantically for its cause. While I believe in cause and effect, I don’t always believe in mankind’s ability to interpret them. Do police officers cause crime because cities with more police have more murders? Surely not, but most arguments aren’t as easy to disentangle. Many people love to dispense advice and pretend they know <strong>why</strong> things happened the way they did. I tend to be more skeptical of this.</p>

<p>I’m aware of but not immune to these faults (neither are you, most likely), so take this with a grain of salt. Take each person’s experience as just that: <strong>their</strong> experience, with <strong>their</strong> mindset, <strong>their</strong> background, and <strong>their</strong> situation. </p>

<p>Also recognize that we aren’t always perfect snowflakes all the time. Have the courage to admit where you need to improve. Now let’s get onto the tips.</p>

<h2>Step 1: Keep a “Feel Good” List</h2>

<p>I don’t remember where I read this, but I love the idea. Keep a simple text file on your computer, mine’s called feelgood.txt. Whenever you have an experience that really makes you happy, put in an entry, even just a few words or a sentence. Here are some of mine:</p>



<pre>
2/4/07: Ron said I was good at thinking of big picture stuff! Remember when we went snowboarding, that was awesome.
1/27/07: I got capistrano + mongrel working! Wow, now I understand what it all means!! Amazing. Crazy. Awesome.
9/12: I got graphs in instacalc!!! Wow!!!
8/31/06: Great toastmasters speech! Enthusiastic! Had good feedback, think I did a good job. Feeling more and more that I can teach, etc. on my own.
</pre>



<p>Some days you’ll have five. Sometimes I’ll go a week or more without writing in it. The goal is to start keeping track of what makes you get excited about life.</p>

<p>Here’s the rub: if you try and sit down and name your favorite things, you might think of a few off the top of your head. But this is in the moment. You’ve forgotten about the things you last week, last month, last year, which have faded in your mind and slipped off the radar.</p>

<p>The feelgood list lets you look back and <strong>see patterns</strong> in what you like. Here are a few patterns I noticed:</p>


<ul>
<li>Intuitively understanding technology</li>
<li>Thinking of new “big-picture” ideas</li>
<li>Improvements in teaching, communication, and education</li>
<li>Creating content that people find useful</li>
</ul>



<p>These trends seem “obvious” to me in hindsight… but how do you <strong>get hindsight</strong> without looking at your past? Our memories aren’t perfect and it’s hard to see trends in your own experience. </p>

<p>The feelgood list is also a great way to pick yourself up when feeling down: reading the old entries recreates the feeling I had at the time. Sometimes a “minor” word of encouragement (from the giver’s perspective) can have a huge effect on you. One small compliment may really stick with you and encourage you try something new or keep going (like Ron’s comment while snowboarding — he likely doesn’t remember).</p>

<p>This can work both ways — an offhand, snide remark can turn you off from a subject forever, so watch out for this. It’s hard to predict what has an impact on you — just write down what does. </p>

<p>Keep a feelgood list. Review it peridiocally. Repeat.</p>

<h2>Step 2: Keep an “Ideas” List</h2>

<p>More lists! Keep another file (ideas.txt, .doc, .xls, .etc) to write down your awesome ideas.</p>

<p>“Uh… what ideas?” you’re probably thinking. You have them, probably a dozen a day. The problem, again, is if I ask you for your ideas you’ll give me a list of 5, the things you thought about just now. That’s not good enough, chump. I want dozens, hundreds.</p>

<p>If you’re like me (all I know is my own experience), your head is constantly buzzing with thought. You’re in line at the grocery store — you’re thinking of stuff. You’re driving — you’re thinking of stuff. You’re “zoning out”, or so it appears — you’re actually thinking of stuff.</p>

<p>You’re doing all this thought, some about new ideas, and nothing is saved. No “Untitled Document (1)” is sitting around in your brain. And that’s a problem.</p>

<p>InstaCalc, BetterExplained, and a few unreleased projects are the result of these idea lists. Sure, not every idea is a winner — but you need to have some in order to pick and choose. Out of 50+ ideas in my list, perhaps 5 or 6 are “winners” that I feel really excited about doing.</p>

<p>And speaking of winners, remember that <strong>the idea isn’t judged when you write it down</strong>. You are capturing version 1 of the thought. If it’s interesting to you, you’ll come back a week later and make version 1.5. Then version 2, then version 10. InstaCalc is probably on version 500 in terms of the number of enhancements and features I’ve added since when I’ve started. And “a faster online calculator” doesn’t sound that sexy as a V1 idea, but it can still become something useful with some iteration.</p>

<p>You can’t get to version 500 if you don’t start with a version 1.</p>

<p>Your ideas are another insight into your mind — the things you think about, enjoy, and want to make better. Most of my ideas are around services that I’d find useful as a programmer or learner, and enhancements to existing tools. They are mostly software-related because that’s my background, though some are around education and communication. </p>

<p>The best part about keeping a list is that your ideas multiply like rabbits in your notebook. Each idea new idea can be combined with each of the old ones, leading to even more variations. Next week when you have yet another idea, it can enhance the older ones and the cycle keeps going.</p>

<p>I also keep a stack of index cards by my bed — I sometimes have brainstorms before going to sleep. I can remember nights where I’d be lying on my pillow and then <strong>bam</strong>, I’d get up a dozen times to write down a thought. It was a bit annoying at the time (”I just want to sleep!!!”) but my brain kept firing. I’m thankful now — most of those thoughts turned into features for InstaCalc.</p>

<p>So <strong>keep a text file and notebook</strong> (for offline use) to track those ideas streaming from your unique snowflake head. I prefer a plain text file because it’s simple, reliable, and fast. I need speed when pumping out thoughts. </p>

<p>The more ideas I write down, the more I seem to have. I’m sure you’ll be the same.</p>

<h2>Step 3: Read the “experts”</h2>

<p>I originally started by reading the experts, but found a lot of value from the feelgood and idea lists. I really value self-learning and I prefer these techniques. </p>

<p>But you’re reading this article, and I’m writing it, so I recognize the role of external tips and advice in getting started. Here’s what helped me:</p>

<p><strong>Understand Your Personality</strong></p>

<p>The point of personality tests isn’t to put you into a box. It’s to recognize the talents you have. The ironic thing is that because <strong>we</strong> find something easy, we assume others do too, and they must not be valuable.</p>

<p>It’s not always the case — some things that come easily to you (talking to strangers, using computers, doing math, writing, humor, relating to others) may be really hard to other people.</p>

<p>These tests can help you step back and realize what you do that others don’t.</p>


<ul>
<li><a href="http://www.amazon.com/exec/obidos/ASIN/0743201140/instacalconli-20">Now, Discover Your Strengths</a> - Understand what you do well and work from there. (My top 5: Strategic, Ideation, Learner, Achiever, Intellection).</li>
<li><a href="http://www.humanmetrics.com/cgi-win/JTypes1.htm">Myers-Briggs test</a> - puts people into one of 16 boxes. I fit best (not “am”, I “fit best”) with <a href="http://www.typelogic.com/intp.html"><span>INTP</span></a>. This has led to <a href="http://www.personalitypage.com/INTP.html">additional insights</a>: “… not likely to place much value on traditional goals such as popularity and security…”.</li>
</ul>



<p><strong>Get Inspired</strong></p>

<p>Sometimes it helps to remember why you are on this quest in the first place — to be happier and live a better life.</p>


<ul>
<li><a href="http://www.stevepavlina.com/">Steve Pavlina</a> has excellent articles on personal development. Lately it’s been a bit new-agey, but I like it overall.</li>
<li><a href="http://betterexplained.com/articles/collected-notes-on-success/">Collected Notes on Success</a> — I’ve collected a few inspirational speeches on pursuing your passions.</li>
<li><a href="http://pf.fastcompany.com/magazine/66/mylife.html">What should I do with my life?</a> helped inspire me to follow my own path when reading it in college.</li>
</ul>



<h2>Parting Thoughts</h2>

<p>The wonderful and frustrating thing about understanding yourself is that nobody can do it for you. After a year of thinking about this seriously, I know what I like (learning and technology), what comes naturally to me (thinking about ideas and intuitive understanding) and what my ideas are about (improvements to software and education). </p>

<p>I had hunches about these things (obvious in hindsight, remember), but thinking about them solidly is very useful.</p>

<p>These insights aren’t set in stone - I don’t want them to be! Some of my greatest joys have been from seemingly random decisions and paths. These clues have given me a fulfilling direction to follow, and I’ll correct the course over time. Software happened to be the first field on the list of what I enjoy, and I’ll continue pursuing it as long as it keeps me excited.</p>

<p>There’ll be more articles on the mechanics of going on your own — a better understanding of what you want is a good staring point. Happy soul-searching. </p></div>
</div>

<div class="feed">
  <div class="title">30. <a href="http://feeds.feedburner.com/~r/Betterexplained/~3/102988938/" target="_blank">How To Debug Web Applications With Firefox</a></div>
  <div class="content"><p> Debugging is one of the most painful parts of developing web apps. You have to deal with browser inconsistencies with <span>HTML, CSS </span>and javascript, let alone the difficulty of debugging javascript itself.</p>

<p>Here’s a rundown of the Firefox extensions I use to manage this madness.</p>

<h2>Taming <span>CSS</span>: Web Developer Toolbar</h2>

<p><a href="https://addons.mozilla.org/firefox/60/">Install Web Developer Toolbar</a>. <a href="https://addons.mozilla.org/firefox/60/">Just do it</a>.</p>

<p>Debugging <span>CSS </span>can be really frustrating. The Web Developer Toolbar lets you inspect and edit (in real-time) the <span>HTML </span>and <span>CSS </span>of your page, so you can see what’s happening when things don’t line up. It can do a heck of a lot more, but here’s what I use it for:</p>

<p><strong>Ctrl + Shift + F</strong>: Display element information. This puts a red box under your mouse. Move the mouse over an element and its attributes appear in a pop-up: the name, class, pixel sizes, fonts, everything. Here’s what you can do:</p>


<ul>
<li>Figure out what classes are creating the styles you see</li>
<li>Easily get the div’s id for use with Firebug (below)</li>
<li>Figure out how big an image is (pixel height and width)</li>
</ul>



<p><img src="http://betterexplained.com/wp-content/uploads/web/web_developer_google.png" alt="web_developer_google.png" title="web_developer_google.png" width="343" height="323" border="0"></p>

<p><strong>Ctrl + Shift + E</strong>: Edit <span>CSS.</span> This pops open a sidebar tab with the current stylesheets. You can edit any attributes and see the effect in real-time (like giving Google a black background):</p>

<p><img src="http://betterexplained.com/wp-content/uploads/web/web_developer_google_black.png" alt="web_developer_google_black.png" title="web_developer_google_black.png" width="460" height="266" border="0"></p>

<p>My favorite <span>CSS </span>style is <strong>border: 1px solid red;</strong></p>

<p>I’ve done the following hundreds of times during the course of web development:</p>


<ul>
<li>Find a div with your mouse (ctrl + shift + f)</li>
<li>Get its id </li>
<li>Edit <span>CSS </span>(ctrl + shift + e)</li>
<li>Put a border on the div: #mydiv{border: 1px solid red;}</li>
<li>Play with widths, heights, margins and paddings until it lines up nicely</li>
<li>Remove the border</li>
</ul>



<p>But rather than <em>deleting</em> the border, put an “x” in front: “xborder: 1px solid red”. The <span>CSS </span>won’t be valid so the border is ignored, but keeps the style around in case you want to enable it later.</p>

<p>Select all the text in your edited <span>CSS </span>file and paste it into the real <span>CSS </span>file. Bam, your changes are now live. It’s almost the reverse of creating a file in DreamWeaver and viewing it in Firefox. You are viewing the live file in Firefox, making changes, and copying those back into your text editor. I’ve found this very effective for editing <span>CSS, </span>you avoid the constant back-and-forth switching because Firefox now has a <span>CSS </span>editor.</p>

<p><strong>Bonus</strong>: ColorZilla Picks Colors</p>

<p><a href="http://www.iosart.com/firefox/colorzilla/">ColorZilla</a> gives you a dropper that can find the hex <span>RGB </span>value (#123456) of anything on the page. This is great when designing, and you want to match a font color to a color in your page. This is way faster than taking a screenshot and opening it up in Photoshop.</p>

<h2>Keeping Javascript In Line: Firebug</h2>

<p><a href="http://www.getfirebug.com/">Firebug, how I love thee</a>. If you love yourself you will install it immediately and save countless hours of frustration.</p>

<p>Firebug can debug javascript, examine the <span>DOM, </span>and do much more (you can and should read all about it). Here’s how I use it:</p>

<p><strong><span>F12</span>:</strong> Open Firebug. You may have to enable it for the page.</p>

<p><strong>Console Tab:</strong> Write quick javascript commands — it even has autocomplete on variable names and properties. Play around with your functions, change <span>CSS </span>attributes, add elements to the page — whatever it takes to test.</p>

<p><strong>Script Tab (Debugging):</strong> Best. Feature. Ever. Click on a line number to set a breakpoint (red dot) in your javascript. Reload the page and it will break (pause) when it encounters the line. </p>

<p>At this point, you can switch over to the console to examine and change variables, and figure out what the heck is going on when your code won’t work. You can then hit the blue “play” button and continue running your app, until the next breakpoint.</p>

<p><strong>Net Tab:</strong> Find the <a href="http://betterexplained.com/articles/speed-up-your-javascript-load-time/">download performance of your page</a>.</p>

<p><strong>Profile Button (on Console Tab):</strong> Find the run-time performance of your page. Click “profile” to begin capturing information, do some commands, and then click stop. You’ll get a report of where your code spends its time. If you must optimize, optimize the common-case first.</p>

<p>If you are a more visual person, try this awesome collage:</p>

<p><img src="http://betterexplained.com/wp-content/uploads/web/firebug_awesome_collage.png" alt="firebug_awesome_collage.png" title="firebug_awesome_collage.png" width="476" height="375" border="0"></p>

<p>Not satisfied? Check out the examples <a href="http://www.getfirebug.com/">on the home page</a>.</p>


<h2>Dive into the details: Live <span>HTTP</span> Headers</h2>

<p>Sometimes you need to dive into the nitty-gritty. What cache headers is my site sending back? Are my pages really gzip-encoded?</p>

<p>I know these questions keep you up at night, so here’s what you can do:</p>

<p>1. <a href="https://addons.mozilla.org/firefox/3829/">Install Live <span>HTTP</span> Headers</a><br>
2. Open it (Tools &gt; Live <span>HTTP</span> Headers)<br>
3. Visit a page / press refresh<br>
4. Rejoice</p>

<p><img src="http://betterexplained.com/wp-content/uploads/web/live_headers.png" alt="live_headers.png" title="live_headers.png" width="204" height="239" border="0"></p>

<p>As you visit a page, you’ll see <span>HTTP </span>headers fly by as your browser requests elements. If items are cached, the browser may not request them at all (awesome!) or may request the element and get a 304 “Not Modified” response (slightly less awesome, you still had to check with the server). I’ll be writing more on cache behavior, but Live <span>HTTP</span> Headers is a great way to learn about <span>HTTP </span>caches (something every webdev should be interested in for performance reasons).</p>

<p>Even better, you can “replay” any header, editing the data that is sent. This is useful when testing or debugging cache or gzip encoding behavior.</p>

<h2>Debugging IE: The lost chapter</h2>

<p>Argh, unfortunately IE lacks these wonderful tools. There is a <a href="http://www.microsoft.com/downloads/details.aspx?familyid=2f465be0-94fd-4569-b3c4-dffdf19ccd99&amp;%23038;displaylang=en">script debugger</a>, but it doesn’t hold a candle to Firebug. In fact, I often just resort to alert statements, which make you shudder after being spoiled by Firebug.</p>

<p>One less painful method I use is this:</p>



<pre>
<code>
In your HTML: &lt;div id=&quot;log&quot;&gt;&lt;/div&gt;

In your Javascript:

function log(str){
  var log = document.getElementById(&quot;log&quot;)
  if (log){ // let&#39;s be safe...
	 log.innerHTML += str + &quot;&lt;br/&gt;&quot;;
  }
}

Usage: log(&quot;Hi there!&quot;);
</code>
</pre>



<p>It’s nothing fancy, just a simple logging function that appends text to a div. Yes, it’s brutal, but it’s better than alert() statements, especially if you have a loop (unless you like repetitive stress injuries or want to condition yourself to fear dialog boxes). If anyone knows a good way to debug javascript in IE I’d love to know. The tools I’ve tried have been very clumsy and disjoint, taking you out of the browser.</p>

<p>I try to do 95% of my development in Firefox, and debug IE-specific issues (like <a href="http://therealcrisp.xs4all.nl/ie7beta/js_substr.html">erratic substr behavior</a>) using this method.</p>

<h2>Keep Getting Better</h2>

<p>Web Developer Toolbar and Firebug can do <strong>way more</strong> than I’ve described here. Like the 80/20 rule, these are commands I use most frequently that give me the best bang for my buck. Take a few minutes to learn these tools and you’ll save <strong>hours</strong> down the line. And here’s a <a href="http://lesliefranke.com/files/firefoxwdev/firefoxwdev.htm">few more tools for web development</a>.</p>

<p>These tools might not save you from getting a nervous twitch in one eye from building web apps, and that’s ok. They’ll save you from getting that twitch in both. </p></div>
</div>

<div class="feed">
  <div class="title">31. <a href="http://feeds.feedburner.com/~r/Betterexplained/~3/106774319/" target="_blank">How To Optimize Your Site With GZIP Compression</a></div>
  <div class="content"><p> Compression is a simple, effective way to save bandwidth and speed up your site. I hesitated when recommending gzip compression when <a href="http://betterexplained.com/articles/speed-up-your-javascript-load-time/">speeding up your javascript</a> because of <a href="http://support.microsoft.com/kb/837251">problems</a> <a href="http://support.microsoft.com/kb/823386">in</a> <a href="http://schroepl.net/projekte/mod_gzip/browser.htm">older browsers</a>.</p>

<p>But it’s 2007. Most of my traffic comes from modern browsers, and quite frankly, most of <a href="http://instacalc.com">my users</a> are fairly tech-savvy. I don’t want to slow everyone else down because somebody is chugging along on IE 4.0 on Windows 95. Google and Yahoo use gzip compression. A modern browser is needed to enjoy modern web content and modern web speed — so gzip encoding it is. Here’s how to set it up.</p>

<h2>Wait, wait, wait: Why are we doing this?</h2>

<p>Before we start I should explain what content encoding is. When you request a file like <code>http://www.yahoo.com/index.html</code>, your browser talks to a web server. The conversation goes a little like this:</p>

<p><img src="http://betterexplained.com/wp-content/uploads/compression/HTTP_request.png" alt="HTTP_request.png" title="HTTP_request.png" width="437" height="221" border="0"></p>

<p>1. Browser: Hey, <strong><span>GET</span></strong> me /index.html<br>
2. Server: Ok, let me see if index.html is lying around…<br>
3. Server: Found it! Here’s your response code (200 OK) and I’m sending the file.<br>
4. Browser: 100KB? Ouch… waiting, waiting… ok, it’s loaded.</p>

<p>Of course, the actual headers and protocols are much more formal (monitor them with <a href="http://betterexplained.com/articles/how-to-debug-web-applications-with-firefox/">Live <span>HTTP</span> Headers</a> if you’re so inclined).</p>

<p>But it worked, and you got your file.</p>

<h2>So what’s the problem?</h2>

<p>Well, the system works, but it’s not that efficient. 100KB is a <strong>lot of text</strong>, and frankly, <span>HTML </span>is redundant. Every <code>&lt;html&gt;, &lt;table&gt; and &lt;div&gt;</code> tag has a closing tag that’s almost the same. Words are repeated throughout the document. Any way you slice it, <span>HTML </span>(and its beefy cousin, <span>XML</span>) is not lean.</p>

<p>And what’s the plan when a file’s too big? Zip it!</p>

<p>If we could send a .zip file to the browser (index.html.zip) instead of plain old index.html, we’d save on bandwidth and download time. The browser could download the zipped file, extract it, and then show it to user, who’s in a good mood because the page loaded quickly. The browser-server conversation might look like this:</p>

<p><img src="http://betterexplained.com/wp-content/uploads/compression/HTTP_request_compressed.png" alt="HTTP_request_compressed.png" title="HTTP_request_compressed.png" width="436" height="213" border="0"></p>

<p>1. Browser: Hey, can I <strong><span>GET</span></strong> index.html? I’ll take a compressed version if you’ve got it.<br>
2. Server: Let me find the file… yep, it’s here. And you’ll take a compressed version? Awesome.<br>
3. Server: Ok, I’ve found index.html (200 OK), am zipping it and sending it over.<br>
4. Browser: Great! It’s only 10KB. I’ll unzip it and show the user.</p>

<p>The formula is simple: Smaller file = faster download = <strong>happy user</strong>. </p>

<p>Don’t believe me? The <span>HTML </span>portion of the yahoo home page goes from 101kb to 15kb after compression:</p>

<p><img src="http://betterexplained.com/wp-content/uploads/compression/yahoo_compression.PNG" alt="yahoo_compression.PNG" title="yahoo_compression.PNG" width="391" height="50" border="0"></p>

<h2>The (not so) hairy details</h2>

<p>The tricky part of this exchange is the browser and server knowing it’s ok to send a zipped file over. The agreement has two parts</p>


<ul>
<li>The <strong>browser sends a header</strong> telling the server it accepts compressed content (gzip and deflate are two compression schemes): <code>Accept-Encoding: gzip, deflate</code></li>
</ul>




<ul>
<li>The <strong>server sends a response</strong> if the content is actually compressed: <code>Content-Encoding: gzip</code></li>
</ul>



<p>If the server doesn’t send the content-encoding response header, it means the file is not compressed (the default on many servers). The “Accept-encoding” header is just a request by the browser, not a demand. If the server doesn’t want to send back compressed content, the browser has to make do with the heavy regular version.</p>

<h2>Setting up the server</h2>

<p>The “good news” is that we can’t control the browser. It either sends the <code>Accept-encoding: gzip, deflate</code> header or it doesn’t. </p>

<p>Our job is to configure the server so it returns zipped content if the browser can handle it, saving bandwidth for everyone (and giving us a happy user). </p>

<p>In Apache, <a href="http://httpd.apache.org/docs/2.0/mod/mod_deflate.html">enabling output compression</a> is fairly straightforward. Add the following to your .htaccess file:</p>



<pre>
<code>
# compress all text &amp; html:
AddOutputFilterByType DEFLATE text/html text/plain text/xml

# Or, compress certain file types by extension:
&lt;Files *.html&gt;
SetOutputFilter DEFLATE
&lt;/Files&gt;
</code>
</pre>



<p>Apache actually has two compression options:</p>


<ul>
<li><strong>mod_deflate</strong> is easier to set up and is standard.</li>
<li><strong>mod_gzip</strong> seems more powerful: you can pre-compress content.</li>
</ul>



<p>Deflate is quick and works, so I use it; use mod_gzip if that floats your boat. In either case, Apache checks if the browser sent the “Accept-encoding” header and returns the compressed or regular version of the file. However, some older browsers may have trouble (more below) and there are special directives you can add to correct this.</p>

<p>If you can’t change your .htaccess file, you can <a href="http://perishablepress.com/press/2007/03/26/fast-effective-php-compression/">use <span>PHP</span></a> to return compressed content. Give your <span>HTML </span>file a .php extension and add this code to the top:</p>



<pre>
<code>
In PHP:
&lt;?php if (substr_count($_SERVER[&#39;HTTP_ACCEPT_ENCODING&#39;], &#39;gzip&#39;)) ob_start(&quot;ob_gzhandler&quot;); else ob_start(); ?&gt;
</code>
</pre>



<p>We check the “Accept-encoding” header and return a gzipped version of the file (otherwise the regular version). This is almost like building your own webserver (what fun!). But really, try to use Apache to compress your output if you can help it. You don’t want to monkey with your files.</p>

<h2>Verify Your Compression</h2>

<p>Once you’ve configured your server, check to make sure you’re actually serving up compressed content.</p>


<ul>
<li><strong>Online:</strong> Use the <a href="http://www.gidnetwork.com/tools/gzip-test.php">online gzip test</a> to check whether your page is compressed.</li>
<li><strong>In your browser:</strong> Use <a href="https://addons.mozilla.org/en-US/firefox/addon/60">Web Developer Toolbar</a> &gt; Information &gt; View Document Size (like I did for Yahoo, above) to see whether the page is compressed.</li>
<li><strong>View the headers:</strong> Use <a href="https://addons.mozilla.org/en-US/firefox/addon/3829">Live <span>HTTP</span> Headers</a> to examine the response. Look for a line that says “Content-encoding: gzip”.</li>
</ul>



<p>Be prepared to marvel at the results. The <a href="http://instacalc.com">instacalc homepage</a> shrunk from 36k to 10k, a 75% reduction in size.</p>

<h2>Try Some Examples</h2>

<p>I’ve set up some pages and a <a href="http://betterexplained.com/examples/compressed/compression-example.zip">downloadable example</a>:</p>


<ul>
<li><a href="http://betterexplained.com/examples/compressed/index.html">index.html</a> - No explicit compression (on this server, I am using compression by default <img src="http://betterexplained.com/wp-includes/images/smilies/icon_smile.gif" alt=":)">  ).</li>
<li><a href="http://betterexplained.com/examples/compressed/index.htm">index.htm</a> - Explicitly compressed with Apache .htaccess using *.htm as a rule</li>
<li><a href="http://betterexplained.com/examples/compressed/index.php">index.php</a> - Explicitly compressed using the <span>PHP </span>header</li>
</ul>



<p>Feel free to download the files, put them on your server and tweak the settings.</p>

<h2>Caveats</h2>

<p>As exciting as it may appear, <span>HTTP</span> Compression isn’t all fun and games. Here’s what to watch out for:</p>


<ul>
<li><strong>Older browsers</strong>: Yes, some browsers still may have trouble with compressed content (they say they can accept it, but really they can’t). If your site absolutely must work with Netscape 1.0 on Windows 95, you may not want to use <span>HTTP</span> Compression. Apache mod_deflate <a href="http://httpd.apache.org/docs/2.0/mod/mod_deflate.html#recommended">has some rules</a> to avoid compression for older browsers.</li>
<li><strong>Already-compressed content</strong>: Most images, music and videos are already compressed. Don’t waste time compressing them again. In fact, you probably only need to compress the “big 3″ (HTML, <span>CSS </span>and Javascript).</li>
<li><strong><span>CPU</span>-load</strong>: Compressing content on-the-fly uses <span>CPU </span>time and saves bandwidth. Usually this is a great tradeoff given the speed of compression. There are ways to pre-compress static content and send over the compressed versions. This requires more configuration; even if it’s not possible, compressing output may still be a net win. Using <span>CPU </span>cycles for a faster user experience is well worth it, given the short attention spans on the web.</li>
</ul>



<p>Enabling compression is one of the fastest ways to improve your site’s performance. Go forth, set it up, and let your users enjoy the benefits. </p></div>
</div>

<div class="feed">
  <div class="title">32. <a href="http://feeds.feedburner.com/~r/Betterexplained/~3/107285397/" target="_blank">How To Optimize Your Site With HTTP Caching</a></div>
  <div class="content"><p> I’ve been on a web tweaking kick lately: how to <a href="http://betterexplained.com/articles/speed-up-your-javascript-load-time/">speed up your javascript</a>, <a href="http://betterexplained.com/articles/how-to-optimize-your-site-with-gzip-compression/">gzip files with your server</a>, and now how to set up caching. But the reason is simple: <strong>site performance is a feature.</strong></p>

<p>For web sites, speed may be feature #1. <strong>Users hate waiting</strong>, we get frustrated by buffering videos and pages that pop together as images slowly load. It’s a jarring (aka bad) user experience. Time invested in site optimization is well worth it, so let’s dive in.</p>

<h2>What is Caching?</h2>

<p>Caching is a great example of the ubiquitous time-space tradeoff in programming. You can <strong>save time</strong> by <strong>using space</strong> to store results.</p>

<p>In the case of websites, the browser can save a copy of images, stylesheets, javascript or the entire page. The next time the user needs that resource (such as a script or logo that appears on every page), the browser doesn’t have to download it again. <strong>Fewer downloads means a faster, happier site.</strong></p>

<p>Here’s a quick refresher on how a web browser gets a page from the server:</p>

<p><img src="http://betterexplained.com/wp-content/uploads/compression/HTTP_request.png" alt="HTTP_request.png" title="HTTP_request.png" width="437" height="221" border="0"></p>

<p>1. Browser: Yo! You got index.html?<br>
2. Server: (Looking it up…)<br>
3. Sever: Totally, dude! It’s right here!<br>
4. Browser: That’s rad, I’m downloading it now and showing the user.</p>

<p>(The actual <span>HTTP </span>protocol may have minor differences; see Live <span>HTTP</span> Headers for more details.)</p>

<h2>Caching’s Ugly Secret: It Gets Stale</h2>

<p>Caching seems fun and easy. The browser saves a copy of a file (like a logo image) and uses this cached (saved) copy on each page that needs the logo. This avoids having to download the image ever again and is perfect, right?</p>

<p>Wrongo. What happens when the company logo changes? Amazon.com becomes Nile.com? Google becomes Quadrillion?</p>

<p>We’ve got a problem. The shiny new logo needs to go with the shiny new site, caches be damned.</p>

<p>So even though the browser has the logo, it doesn’t know whether the image can be used. After all, the file may have changed on the server and there could be an updated version.</p>

<p>So why bother caching if we can’t be sure if the file is good? Luckily, there’s a few ways to fix this problem. </p>

<h2>Caching Method 1: Last-Modified</h2>

<p>One fix is for the server to tell the browser <strong>what version</strong> of the file it is sending. A server can return a <code>Last-modified</code> date along with the file (let’s call it logo.png), like this:</p>

<p><code>Last-modified: Fri, 16 Mar 2007 04:00:25 GMT</code><br>
<code>File Contents (could be an image, HTML, CSS, Javascript...)</code></p>

<p>Now the browser knows that the file it got (logo.png) was created on Mar 16 2007. The next time the browser needs logo.png, it can do a special check with the server:</p>

<p><img src="http://betterexplained.com/wp-content/uploads/compression/HTTP-caching-last-modified_1.png" alt="HTTP-caching-last-modified_1.png" title="HTTP-caching-last-modified_1.png" width="440" height="216" border="0"></p>

<p>1. Browser: Hey, give me logo.png, but only if it’s been modified since Mar 16, 2007.<br>
2. Sever: (Checking the modification date)<br>
3. Server: Hey, you’re in luck! It was <strong>not modified</strong> since that date. You have the latest version.<br>
4. Browser: Great! I’ll show the user the cached version.</p>

<p>Sending the short “Not Modified” message is a <strong>lot faster</strong> than needing to download the file again, especially for giant javascript or image files. Caching saves the day (err… the bandwidth).</p>

<h2>Caching Method 2: ETag</h2>

<p>Comparing versions with the modification time generally works, but could lead to problems. What if the server’s clock was originally wrong and then got fixed? What if daylight savings time comes early and the server isn’t updated? The caches could be inaccurate.</p>

<p>ETags to the rescue. An ETag is a unique identifier given to every file. It’s like a hash or fingerprint: every file gets a unique fingerprint, and if you change the file (even by one byte), the fingerprint changes as well.</p>

<p>Instead of sending back the modification time, the server can send back the ETag (fingerprint):</p>

<p><code>ETag: ead145f</code><br>
<code>File Contents (could be an image, HTML, CSS, Javascript...)</code></p>

<p>The ETag can be any string which uniquely identifies the file. The next time the browser needs logo.png, it can have a conversation like this:</p>

<p><img src="http://betterexplained.com/wp-content/uploads/compression/HTTP_caching_if_none_match.png" alt="HTTP_caching_if_none_match.png" title="HTTP_caching_if_none_match.png" width="441" height="219" border="0"></p>

<p>1. Browser: Can I get logo.png, if nothing matches tag “ead145f”?<br>
2. Server: (Checking fingerprint on logo.png)<br>
3. Server: You’re in luck! The version here is “ead145f”. It was <strong>not modified</strong>.<br>
4. Browser: Score! I’ll show the user my cached version.</p>

<p>Just like last-modifed, <strong>ETags solve the problem of comparing file versions</strong>, except that “if-none-match” is a bit harder to work into a sentence than “if-modified-since”. But that’s my problem, not yours. ETags work great.</p>

<h2>Caching Method 3: Expires</h2>

<p>Caching a file and checking with the server is nice, except for one thing: <strong>we are still checking with the server.</strong> It’s like analyzing your milk every time you make cereal to see whether it’s safe to drink. Sure, it’s better than buying a new gallon each time, but it’s not exactly wonderful.</p>

<p>And how do we handle this milk situation? With an <strong>expiration date!</strong></p>

<p>If we know when the milk (logo.png) expires, we keep using it until that date (and maybe a few days longer, if you’re a college student). As soon as it goes expires, we contact the server for a fresh copy, with a new expiration date. The header looks like this:</p>

<p><code>Expires: Tue, 20 Mar 2007 04:00:25 GMT</code><br>
<code>File Contents (could be an image, HTML, CSS, Javascript...)</code></p>

<p>In the meantime, we avoid even talking to the server if we’re in the expiration period:</p>

<p><img src="http://betterexplained.com/wp-content/uploads/compression/HTTP_caching_expires.png" alt="HTTP_caching_expires.png" title="HTTP_caching_expires.png" width="419" height="220" border="0"></p>

<p>There isn’t a conversation here; the browser has a monologue.</p>

<p>1. Browser: Self, is it before the expiration date of Mar 20, 2007? (Assume it is).<br>
2. Browser: Verily, I will show the user the cached version.</p>

<p>And that’s that. The web server didn’t have to do anything. The user sees the file instantly.</p>

<h2>Caching Method 4: Max-Age</h2>

<p>Oh, we’re not done yet. Expires is great, but it has to be computed for every date. The <code>max-age</code> header lets us say “This file expires 1 week from today”, which is simpler than setting an explicit date.</p>

<p>Max-Age is measured in seconds. Here’s a few <a href="http://instacalc.com/?d=&amp;%23038;c=MSBkYXkgaW4gc2Vjb25kc3wxIHdlZWsgaW4gc2Vjb25kc3wxIG1vbnRoIGluIHNlY29uZHN8MSB5ZWFyIGluIHNlY29uZHN8fHw&amp;%23038;s=sssssss&amp;%23038;v=0.9">quick second conversions</a>:</p>


<ul>
<li>1 day in seconds = 86400</li>
<li>1 week in seconds = 604800</li>
<li>1 month in seconds = 2629000</li>
<li>1 year in seconds = 31536000 (effectively infinite on internet time)</li>
</ul>



<h2>Bonus Header: Public and Private</h2>

<p>The cache headers never cease. Sometimes a server needs to control when certain resources are cached. </p>


<ul>
<li><code>Cache-control: public</code> means the cached version can be saved by proxies and other intermediate servers, where everyone can see it.</li>
<li><code>Cache-control: private</code> means the file is different for different users (such as their personal homepage). The user’s private browser can cache it, but not public proxies.</li>
<li><code>Cache-control: no-cache</code> means the file should not be cached. This is useful for things like search results where the <span>URL </span>appears the same but the content may change.</li>
</ul>



<p>However, be wary that some cache directives only work on newer <span>HTTP</span> 1.1 browsers. If you are doing special caching of authenticated pages then <a href="http://www.mnot.net/cache_docs/">read more about caching</a>.</p>

<h2>Ok, I’m Sold: Enable Caching</h2>

<p>We’ve seen the following headers that really help our caching:</p>


<ul>
<li><code>Last-modified: </code></li>
<li><code>ETag: </code></li>
<li><code>Expires: </code></li>
<li><code>Cache-control: max-age=86400</code></li>
</ul>



<p>Now let’s put it all together and get Apache to return the right headers. If your resource changes:</p>


<ul>
<li>Daily or more: Use last-modifed or ETag. Apache does this for you automatically!</li>
<li>Weekly-monthly: Use <code>max-age</code> for a day or week. Put the .htaccess file in the directory you want to cache:</li>
</ul>




<pre>
<code>
#Create filter to match files you want to cache 
&lt;Files *.js&gt;
Header add &quot;Cache-Control&quot; &quot;max-age=604800&quot;
&lt;/Files&gt;
</code>
</pre>




<ul>
<li>Never changes: Use <code>max-age=31536000</code> and <a href="http://www.thinkvitamin.com/features/webapps/serving-javascript-fast">really far-out expires headers</a>.</li>
</ul>





<pre>
<code>
&lt;Files *.js&gt;
Header add &quot;Expires&quot; &quot;Mon, 28 Jul 2014 23:30:00 GMT&quot;
Header add &quot;Cache-Control&quot; &quot;max-age=31536000&quot;
&lt;/Files&gt;
</code>
</pre>



<p>How can a file never change? Simple. <strong>Put different versions of the file in different directories.</strong></p>

<p>For instacalc, I keep the core files of each build in a unique directory, such as “build490″. When I’m using build490, index.html pulls all images, stylesheets, and javascripts from that directory. I can cache the the files in build490 <strong>forever</strong> because build490 will never change. </p>

<p>If I have a new version (build491… how creative), index.html will point to that folder instead. I’ve created scripts to take care of this find/replace housekeeping, though you can use <span>URL </span>rewriting rules as well. I prefer to have the <span>HTML </span>point to the actual file.</p>

<p>Remember that index.html <strong>cannot be cached forever</strong>, since it changes every now and then to point to new directories. So for the “loader” file, I’m using the regular Last-Modified caching strategy. I think it’s fine to have that small “304 Not Modified” communication with the server — we still avoid sending requests for all the files in the build490 folder. If you want, monkey around and give the index.html file a small expiration (say a few hours).</p>

<h2>Final Step: Check Your Caching</h2>

<p>To see whether your files are cached, do the following:</p>


<ul>
<li>Online: Examine your site in the <a href="http://www.surfnetters.nl/cgi-bin/cacheability.py">cacheability query</a> (green means cacheable)</li>
<li>In Browser: Use <a href="http://betterexplained.com/articles/how-to-debug-web-applications-with-firefox/">FireBug or Live <span>HTTP</span> Headers</a> to see the <span>HTTP </span>response (304 Not Modified, Cache-Control, etc.). In particular, I’ll load a page and use Live <span>HTTP</span> Headers to make sure no packets are being sent to load images, logos, and other cached files. If you press ctrl+refresh the browser will force a reload of all files.</li>
</ul>



<p>Read more about <a href="http://www.mnot.net/cache_docs/">caching</a>, or the <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html"><span>HTTP </span>header fields</a>. Caching doesn’t help with the initial download (that’s what gzip is for), but it makes the overall site experience much better. </p>

<p><strong>Remember: Creating unique <span>URL</span>s is the simplest way to caching heaven.</strong> Have fun streamlining your site! </p></div>
</div>

<div class="feed">
  <div class="title">33. <a href="http://feeds.feedburner.com/~r/Betterexplained/~3/111493320/" target="_blank">The Quick Guide to GUIDs</a></div>
  <div class="content"><p> Our world is numbered. Books have <span>ISBN</span>s and products have barcodes. Cars have <a href="http://en.wikipedia.org/wiki/Vehicle_identification_number"><span>VIN</span>s</a>, even people have social security numbers.</p>

<p>Numbers help us reference items unambiguously. “John Smith” may be many people, but Social Security Number 123-45-6789 refers to one person exactly.</p>

<p>A <span>GUID </span>(globally unique identifier) is a bigger, badder version of this type of ID number. You may see the term <span>UUID </span>tossed about (universally unique identifier), a nitpicky word for those whose numbers are unique not only within the globe, but throughout the entire universe.</p>

<p>Any way you title it, <span>GUID</span>s or <span>UUID</span>s are just big, gigantic ID numbers.</p>

<h2>The Problem With Counting</h2>

<p>“We don’t need no stinkin’ <span>GUID</span>s,” you may be thinking between gulps of <a href="http://en.wikipedia.org/wiki/Instant_noodles">Top Ramen</a>, “I’ll just use regular numbers and start counting up from 1.”</p>

<p>Sure, it sounds easy. Just start with <span>ISBN </span>#1 and add one for each new book. But problems arise:</p>


<ul>
<li>Who does the counting? A central authority?</li>
<li>Who handles simulataneous requests and eliminates duplicates?</li>
<li>Can IDs be shared between products? Is Social Security Number 1 different from <span>ISBN</span> 1?</li>
<li>Can people guess what the next ID will be? How many IDs have been issued?</li>
</ul>



<p>The problem with counting is that we want to <strong>create ID numbers without the management headache</strong>.</p>

<h2><span>GUID</span>s to the Rescue</h2>

<p><span>GUID</span>s are large, enormous numbers that are nearly guaranteed to be unique. They are usually 128 bits long and look like this <a href="http://betterexplained.com/articles/numbers-and-bases/">in hexadecimal</a>:</p>

<p><code>
30dd879c-ee2f-11db-8314-0800200c9a66
</code></p>

<p>The format is a well-defined sequence of 32 hex digits grouped into chunks of 8-4-4-4-12. This gives us <code>2^128</code> or about <a href="http://instacalc.com/?d=&amp;%23038;c=Ml4xMjh8fHx8fHw&amp;%23038;s=sssssss&amp;%23038;v=0.9">10^38 numbers</a>.</p>

<p>Here’s the thinking behind <span>GUID</span>s:</p>


<ul>
<li>If you pick a huge random number (38 digits long), it’s <strong>really unlikely</strong> that someone will pick the same one.</li>
</ul>




<ul>
<li><span>GUID</span>s are not tied to a product. A <span>GUID </span>can be used for people, cars, files, webpages, colors, anything. With regular registration numbers, you start counting at 1 and numbers can overlap. Social Security Number 123-45-6789 is different from <span>ISBN</span> 123456789 which is different from barcode 123456789. This isn’t an issue with <span>GUID</span>s.</li>
</ul>




<ul>
<li>It’s up to the person reading the <span>GUID </span>to figure out the context of the <span>GUID.</span> There are so many <span>GUID</span>s that you can use them to number everything and not run out.</li>
</ul>



<p><strong><span>GUID</span>s give you a unique serial number that can be used on any item in the universe.</strong></p>

<h2>The Great <span>GUID</span> Shortage</h2>

<p>When learning about <span>GUID</span>s, it feels like 38 measly digits aren’t enough. Won’t we run out if people get <span>GUID</span>-crazy, assigning them for everything from their pets to their favorite bubble gum flavor?</p>

<p>Let’s see. Think about how big the Internet is: Google has billions of web pages in its index. Let’s call it a trillion (10^12) for kicks. Think about every wikipedia article, every news item on <span>CNN, </span>every product in Amazon, every blog post from any author. We can assign a <span>GUID </span>for each of these documents.</p>

<p>Now let’s say everyone on Earth gets their own copy of the internet, to keep track of their stuff. Even crazier, let’s say each person gets their own copy of the internet every second. How long can we go on?</p>

<p><a href="http://instacalc.com/?d=V2lsbCB3ZSBydW4gb3V0IG9mIEdVSURzPw&amp;%23038;c=Z3VpZHMgPSAyXjEyOHxwb3B1bGF0aW9uID0gNi42IGJpbGxpb258aW50ZXJuZXQgPSAxIHRyaWxsaW9uIGRvY3VtZW50c3xndWlkcyAvIHBvcHVsYXRpb24gLyBpbnRlcm5ldHxyNCBzZWNvbmRzIGluIHllYXJzfC8vIHdlIGNhbiBnbyBmb3IgMS42IGJpbGxpb24geWVhcnN8fHx8fHw&amp;%23038;s=ssssssssssss&amp;%23038;v=0.9">Over a billion years</a>. </p>

<p>Let me say that again. Each person gets a <strong>personal copy of the internet, every second, for a billion years</strong>. </p>

<p>It’s a mind-boggling amount of items, and it’s hard to get our heads around it. Trust me, we won’t run out of <span>GUID</span>s anytime soon. And if we do? We’ll start using <span>GUID</span>s with more digits.</p>

<h2>Using <span>GUID</span>s</h2>

<p>If you want to create <span>GUID</span>s, try the</p>


<ul>
<li><a href="http://www.famkruithof.net/uuid/uuidgen">Online <span>GUID</span> Generator</a></li>
<li><span>GUID </span>libraries for <a href="http://us.php.net/uniqid"><span>PHP</span></a>, <a href="http://search.cpan.org/~rjbs/Data-UUID-1.148/UUID.pm">Perl</a>, <a href="http://raa.ruby-lang.org/project/uuid/">Ruby</a>, <a href="http://docs.python.org/lib/module-uuid.html">Python</a>, <a href="http://msdn2.microsoft.com/en-us/library/system.guid.aspx">.NET</a></li>
<li><a href="http://tools.assembla.com/breakout/wiki/FreeSoftware">usesguid plugin</a> for Ruby on Rails: Use a <span>GUID </span>instead of an integer as a primary key in your database.</li>
</ul>



<p>There are several ways to create <span>GUID</span>s (<a href="http://www.ietf.org/rfc/rfc4122.txt"><span>RFC</span> 4122</a> describes the conventions), but you want to avoid that mess and use a library. The general types of <span>GUID</span>s are:</p>


<ul>
<li><strong>Random:</strong> Just use the system’s random-number generator to create a 128-bit number.</li>
<li><strong>Time-based:</strong> Create a <span>GUID </span>based on the current time.</li>
<li><strong>Hardware-based:</strong> Make a <span>GUID </span>with certain portions based on hardware features, such as the <span>MAC </span>address of a network card. This isn’t great because the <span>GUID </span>isn’t “anonymous” and can be partially traced to the machine that created it.</li>
<li><strong>Content-based (MD5 or <span>SHA</span>-1 hash of data):</strong> Create a <span>GUID </span>based on a hash of the file contents. Files with the same contents will get the same <span>GUID.</span> You can also seed the hash with a unique namespace (like your <span>URL</span>).</li>
</ul>



<p>You can mix-and-match techniques above. If you want duplicate files to have the same <span>GUID, </span>then use <span>GUID</span>s based on the contents. If you want <span>GUID</span>s to be unique, even if the contents are the same, then create them randomly or with a combination of file contents and a random number.</p>

<h2><span>GUID</span> Examples</h2>

<p>Here’s a few things you can do with <span>GUID</span>s:</p>


<ul>
<li>Unique primary key in databases. This lets database items created on separate machines be merged later without conflict, and without the need for a central server to manage IDs.</li>
<li>Unique filename for uploaded files (such as Windows Defender on <a href="http://www.microsoft.com/downloads/details.aspx?displaylang=en&amp;%23038;FamilyId=435BFCE7-DA2B-4A6A-AFA4-F7F14E605A0D">Microsoft Download Center</a>). If each version of the file gets its own <span>GUID, </span>you can set a <a href="http://betterexplained.com/articles/how-to-optimize-your-site-with-http-caching/">long cache expiration time</a>.</li>
<li>Unique name for resources (del.icio.us <span>URL </span>for instacalc: <a href="http://del.icio.us/url/6c5ff0ed608e75724df94a52b05dd6a8">http://del.icio.us/url/6c5ff0ed608e75724df94a52b05dd6a8</a>) </li>
<li>Allow vendors to create and register unique IDs without contacting a central authority (like <a href="http://en.wikipedia.org/wiki/Component_Object_Model">class IDs in <span>COM</span></a>)</li>
</ul>



<h2>The Problem with <span>GUID</span>s</h2>

<p>Like most things in life, <span>GUID</span>s have benefits and drawbacks. Weigh the features to see if they make sense:</p>


<ul>
<li><strong>No central authority:</strong> You avoid the need for management, but can’t keep track of what’s been assigned. A compromise is to generate <span>GUID</span>s internally and then hand them out.</li>
<li><strong>Appear random:</strong> Users cannot easily guess the ID for an object they don’t know. This is good for security, difficult for debugging.</li>
<li><strong>Easily combined:</strong> You can merge <span>GUID</span>s from different data sources very easily with a low chance of conflict.</li>
<li><strong><span>GUID </span>overhead:</strong> <span>GUID</span>s are an example of the time-space tradeoff. You save time in merging but have to use space to store the large (16-byte) <span>GUID.</span> It may not make sense to have a 16-byte <span>GUID </span>keeping track of a 4-byte item in your database.</li>
</ul>



<h2><span>GUID</span>s are not a <span>GUAR</span>antee</h2>

<p>There’s one giant caveat for <span>GUID</span>s: collisions are still possible.</p>

<p>First, the <a href="http://betterexplained.com/articles/understanding-the-birthday-paradox/">birthday paradox</a> shows us the chance of a collision as <a href="http://en.wikipedia.org/wiki/Uuid#Random_UUID_Probability_of_Duplicates"><span>GUID</span>s are used</a>.  It’s very, very unlikely that <span>GUID</span>s will collide, but as more are assigned, there are fewer left to choose from.</p>

<p>Second, a malicious user could try hijacking <span>GUID</span>s that he knows will be used (assuming the user can assign their own <span>GUID</span>s), or resubmitting different content to a previous <span>GUID </span>(submitting file A under the hash of file B).</p>

<p>If you are writing software, program defensively and detect cases where the <span>GUID </span>already exists. Give the user an error or even better, recover, create a new <span>GUID </span>on the server side and try again. <span>GUID</span>s are great, but they aren’t a magic bullet.</p>

<p>As always, we’re never done learning. Read more about <span>GUID</span>s here:</p>


<ul>
<li><a href="http://www.ietf.org/rfc/rfc4122.txt" title="RFC 4122">A Universally Unique IDentifier (UUID) <span>URN</span> Namespace</a></li>
<li>Coding Horror: <a href="http://www.codinghorror.com/blog/archives/000817.html">Primary Keys vs. <span>GUID</span>s</a></li>
<li>Wikipedia on <a href="http://en.wikipedia.org/wiki/Guid"><span>GUID</span>s</a> and <a href="http://en.wikipedia.org/wiki/Uuid"><span>UUID</span>s</a> </li>
</ul></div>
</div>

<div class="feed">
  <div class="title">34. <a href="http://feeds.feedburner.com/~r/Betterexplained/~3/112106049/" target="_blank">Understanding the Birthday Paradox</a></div>
  <div class="content"><p> <strong>23 people</strong>. In a room of just 23 people there’s a 50-50 chance of two people having the same birthday. In a room of 75 there’s a 99.9% chance of two people matching.</p>

<p>Put down the calculator and pitchfork, I don’t speak heresy. The birthday paradox is strange, counter-intuitive, and <strong>completely true</strong>. It’s only a “paradox” in the sense that our brains can’t really handle the power of exponents and expect probabilities to be linear.</p>

<p>Let’s wrap our heads around why the paradox happens and how it works.</p>

<h2>Problem 1: Exponents aren’t intuitive</h2>

<p>We’ve taught ourselves mathematics and statistics, but let’s not kid ourselves: it’s not natural.</p>

<p>Here’s an example: What’s the chance of getting 10 heads in a row when flipping coins? The untrained brain might think like this:</p>

<p>“Well, getting one head is a 50% chance. Getting two heads is twice as hard, so a 25% chance. Getting <strong>ten</strong> heads is probably 10 times harder… so about 50%/10 or a 5% chance.”</p>

<p>And there we sit, smug as a bug on a rug. No dice bub.</p>

<p><strong>After pounding your head with statistics</strong>, you know not to divide, but use <strong>exponents</strong>. The chance of 10 heads is not .5/10 but .5^10, or about .0001.</p>

<p>But even after training, we get caught again. At 5% interest we’ll double our money in 14 years, rather than the “expected” 20. Did you naturally infer the <a href="http://betterexplained.com/articles/the-rule-of-72/">Rule of 72</a> when learning about interest rates? Probably not. Understanding compound exponential growth with our linear brains is hard.</p>

<h2>Problem 2: Humans are a tad bit selfish</h2>

<p>Take a look at the <a href="http://news.google.com">news</a>. Notice how much of the negative news is the result of acting without considering others. I’m an optimist and <em>do</em> have hope for mankind, but that’s a separate discussion <img src="http://betterexplained.com/wp-includes/images/smilies/icon_smile.gif" alt=":)"> .</p>

<p>In a room of 23, do you think of the 22 comparisons where <strong>your</strong> birthday is being compared against someone else’s? Probably.</p>

<p>Do you think of the <strong>231</strong> comparisons where someone you don’t know is being checked against someone else you don’t know? Do you realize there are so many? Probably not.</p>

<p>The fact that we neglect the <strong>10 times as many</strong> comparisons that don’t include us helps us see why the “paradox” can happen.</p>

<h2>Ok, fine, humans are awful: Show me the math!</h2>

<p>Ok, here’s the problem: What are the chances that two people share a birthday in a group of 23?</p>

<p>Sure, we could list the pairs and count all the ways they could match. But that’s hard: there could be 1, 2, 3 or even 23 matches!</p>

<p>It’s like asking “What’s the chance of getting one or more heads in 23 coin flips?” There are so many possibilities: heads on the first throw, or the 3rd, or the last, or the 1st and 3rd, the 2nd and 21st, and so on.</p>

<p>How do we solve the coin problem? Flip it around (Get it? Get it?). Rather than counting every way to get heads, <strong>find the chance of getting all tails, our “problem scenario”</strong>.</p>

<p>If there’s a 1% chance of getting all tails (more like .5^23 but work with me here), there’s a 99% chance of having <strong>at least one head</strong>. I don’t know if it’s 1 head, or 2, or 15 or 23: we got heads, and that’s what matters. If we subtract the chance of a problem scenario from 1 we are left with the probability of a good scenario.</p>

<p>The same principle applies for birthdays. Instead of finding all the ways we match, <strong>find the chance that everyone is different, the “problem scenario”</strong>. We then take the opposite probability and get the chance of a match. It may be 1 match, or 2, or 20, but somebody matched, which is what we need to find.</p>

<h2>Explanation: Counting Pairs</h2>

<p>With 23 people we have 253 pairs:</p>

<p><img src="http://betterexplained.com/latexrender/pictures/342da5d50fb2e1f6b6dec572354d482a.gif" title="\displaystyle{\frac{23 \cdot 22}{2} = 253}" alt="\displaystyle{\frac{23 \cdot 22}{2} = 253}"></p>

<p>(Brush up on <a href="http://betterexplained.com/articles/easy-permutations-and-combinations/">combinations and permuations</a> if you like).</p>

<p>The chance of 2 people having different birthdays is:</p>

<p><img src="http://betterexplained.com/latexrender/pictures/a6704954961ee4b46f25aa989f10e099.gif" title="\displaystyle{1 - \frac{1}{365} = \frac{364}{365} = .997260}" alt="\displaystyle{1 - \frac{1}{365} = \frac{364}{365} = .997260}"></p>

<p>Makes sense, right? There’s 364 out of 365 birthdays that are “OK”.</p>

<p>Having all <strong>253 pairs</strong> be different is like getting heads 253 times in a row (well, sort-of: let’s assume birthdays are independent). We use exponents to find the probability:</p>

<p><img src="http://betterexplained.com/latexrender/pictures/7c815fd10739f8e604442ea7d0342f28.gif" title="\displaystyle{\left(\frac{364}{365}\right)^{253} = .4995}" alt="\displaystyle{\left(\frac{364}{365}\right)^{253} = .4995}"></p>

<p>99.7260% is really close to one, but when you multiply it 251 times it shrinks. Really fast.</p>

<p>The chance that we have a match is: 1 - 49.95% = 50.05%, or just over half! If you want to find the probability of a match for any number of people n the formula is:</p>

<p><img src="http://betterexplained.com/latexrender/pictures/b19f67d82cee00406b5b1f17901dd1d1.gif" title="\displaystyle{p(n) = 1 - \left(\frac{364}{365}\right)^{C(n,2)} = 1 - \left(\frac{364}{365}\right)^{n(n-1)/2} }" alt="\displaystyle{p(n) = 1 - \left(\frac{364}{365}\right)^{C(n,2)} = 1 - \left(\frac{364}{365}\right)^{n(n-1)/2} }"></p>

<h2>Interactive Example</h2>

<p>I didn’t believe we needed only 23 people. The math works out, but is it real?</p>

<p>You bet. Try the example below: Pick a number of items (365), a number of people (23) and run a few trials. You’ll see the theoretical match <span>and your actual match </span> as you run your trials. Go ahead, click the button (or <a href="http://betterexplained.com/examples/birthday/birthday.html">see the full page</a>).</p>

<p></p>

<p>As you run more and more trials (keep clicking!) the actual probability should approach the theoretical one.</p>

<h2>Examples and Takeaways</h2>

<p>Here are a few lessons from the birthday paradox:</p>


<ul>
<li><strong>sqrt(n)</strong> is roughly the number you need to have a 50% chance of a match with n items. sqrt(365) is about 20. This comes into play in cryptography for the birthday attack.</li>
<li>Even though there are 2<sup>128</sup> (1e38) <a href="http://betterexplained.com/articles/the-quick-guide-to-guids/"><span>GUID</span>s</a>, we only have 2<sup>64</sup> (1e19) to use up before a 50% chance of collision. And 50% is really, really high.</li>
<li>You only need 13 people picking letters of the alphabet to have 95% chance of a match. Try it above (people = 13, items = 26).</li>
<li>Exponential growth rapidly decreases the chance of picking unique items (aka it increases the chanes of a match). Remember: exponents are non-intuitive and humans are selfish! <img src="http://betterexplained.com/wp-includes/images/smilies/icon_smile.gif" alt=":)"> </li>
</ul>



<p>After thinking about it a lot, the birthday paradox finally clicks with me. But I still check out the interactive example just to make sure.</p>

<h2>Appendix A: Repeated Multiplication Explanation (Geeky Math Alert!)</h2>

<p>Remember how we assumed birthdays are independent? Well, they aren’t. </p>

<p>If Person 1 and Person 3 match, and Person 3 and 5 match, we know that 1 and 5 match also. The outcome of 1 and 5 depends on their results with 3, which means the results aren’t an independent 1/365 chance (in our case, it’s a 100% chance of a match).</p>

<p>When counting pairs we did math as if birthdays were like independent coin flips, and multiplied probabilities. This assumption isn’t strictly true but it’s “good enough” for a small number of people (23) compared to the sample size (365). It’s unlikely to have multiple people match and screw up the independence, so it’s a good approximation.</p>

<p>It’s unlikely, but it can happen. Let’s figure out the real chances of each person picking a different number:</p>


<ul>
<li>The first person has a 100% chance of a unique number (of course)</li>
<li>The second has a (1 - 1/365) chance (all but 1 number from the 365)</li>
<li>The third has a (1 - 2/365) chance (all but 2 numbers)</li>
<li>The 23rd has a (1 - 22/365) (all but 22 numbers)</li>
</ul>



<p>The multiplication looks pretty ugly:</p>

<p><img src="http://betterexplained.com/latexrender/pictures/867ad9a64d2c234aa4ac44ce58e3e63d.gif" title="\displaystyle{p(different) = 1 \cdot \left(1-\frac{1}{365}\right) \cdot \left(1-\frac{2}{365}\right)  \cdots \left(1-\frac{22}{365}\right)}" alt="\displaystyle{p(different) = 1 \cdot \left(1-\frac{1}{365}\right) \cdot \left(1-\frac{2}{365}\right)  \cdots \left(1-\frac{22}{365}\right)}"></p>

<p>But there’s a shortcut we can take. A coarse first-order Taylor approximation for e^x is:</p>

<p><img src="http://betterexplained.com/latexrender/pictures/db7e38c1814620c167d4e1204600aaef.gif" title="\displaystyle{e^x  \approx 1 + x}" alt="\displaystyle{e^x  \approx 1 + x}"></p>

<p>so</p>

<p><img src="http://betterexplained.com/latexrender/pictures/768103c5390603209b7cf5b78fdcf871.gif" title="\displaystyle{ 1 - \frac{1}{365} \approx e^{-1/365}}" alt="\displaystyle{ 1 - \frac{1}{365} \approx e^{-1/365}}"></p>

<p>Using our handy shortcut we can rewrite the big equation to:</p>

<p><img src="http://betterexplained.com/latexrender/pictures/ea9e96883683e70eac5c56e555ac5779.gif" title="\displaystyle{p(different) \approx 1 \cdot e^{-1/365} \cdot e^{-2/365} \cdots e^{-22/365}}" alt="\displaystyle{p(different) \approx 1 \cdot e^{-1/365} \cdot e^{-2/365} \cdots e^{-22/365}}"></p>

<p><img src="http://betterexplained.com/latexrender/pictures/8507032654ac39235fc5c15e9cf60bd3.gif" title="\displaystyle{p(different) \approx e^{(-1 -2 -3 ... -22)/365}}" alt="\displaystyle{p(different) \approx e^{(-1 -2 -3 ... -22)/365}}"></p>

<p><img src="http://betterexplained.com/latexrender/pictures/ac8c1b13881dc4522fb0ce83507cf74b.gif" title="\displaystyle{p(different) \approx e^{-(1 + 2 + ... 22)/365}}" alt="\displaystyle{p(different) \approx e^{-(1 + 2 + ... 22)/365}}"></p>

<p>But we remember that <a href="http://betterexplained.com/articles/techniques-for-adding-the-numbers-1-to-100/">adding the numbers 1 to n</a> = n(n + 1)/2. Don’t confuse this with n(n-1)/2, which is C(n,2) or the number of pairs of n items. They look almost the same!</p>

<p>Adding 1 to 22 is (22 * 23)/2 so we get:</p>

<p><img src="http://betterexplained.com/latexrender/pictures/c6be9cf1ea6cc7da98ed1b62a14ed6a9.gif" title="\displaystyle{p(different) \approx e^{-(23 \cdot 22 / 2 \cdot 365)} = .499998}" alt="\displaystyle{p(different) \approx e^{-(23 \cdot 22 / 2 \cdot 365)} = .499998}"></p>

<p>Phew. This approximation is <a href="http://instacalc.com/?d=Q2FsY3VsYXRpbmcgdGhlIGFwcHJveGltYXRlIGFuZCBhY3R1YWwgcHJvYmFiaWxpdGllcyBmb3IgdGhlIGJpcnRoZGF5IHBhcmFkb3gu&amp;%23038;c=cG93ZXIgPSAtKDIzKjIyKS8oMiogMzY1KXx8ZXN0aW1hdGVkID0gRV5wb3dlcnxhY3R1YWwgPSAoMzY0LzM2NSleMjUxfGVycm9yX21hcmdpbiA9IChlc3RpbWF0ZWQgLSBhY3R1YWwpIC8gYWN0dWFsfHw&amp;%23038;s=sssssss&amp;%23038;v=0.9">very close</a> and good enough for government work, as they say. If you simplify the formula a bit and swap in <em>n</em> for 23 you get:</p>

<p><img src="http://betterexplained.com/latexrender/pictures/caa741bff71c448bd5832bafb6c6065e.gif" title="\displaystyle{p(different) \approx e^{-(n^2 / 2 \cdot 365)}}" alt="\displaystyle{p(different) \approx e^{-(n^2 / 2 \cdot 365)}}"></p>

<p>and</p>

<p><img src="http://betterexplained.com/latexrender/pictures/59adc22e225e9a48de8ea1470f846b2c.gif" title="\displaystyle{p(match) = 1 - p(different) \approx 1 - e^{-(n^2 / 2 \cdot 365)}}" alt="\displaystyle{p(match) = 1 - p(different) \approx 1 - e^{-(n^2 / 2 \cdot 365)}}"></p>

<h2>Appendix B: The General Birthday Formula</h2>

<p>Let’s generalize the formula to picking <em>n</em> people from <em>T</em> total items (instead of 365):</p>

<p><img src="http://betterexplained.com/latexrender/pictures/f96517aec2f4183513f957925811fb27.gif" title="\displaystyle{p(different) \approx e^{-(n^2 / 2 \cdot T)}}" alt="\displaystyle{p(different) \approx e^{-(n^2 / 2 \cdot T)}}"></p>

<p>If we choose a probability (like 50% chance of a match) and solve for <em>n</em>:</p>

<p><img src="http://betterexplained.com/latexrender/pictures/f96517aec2f4183513f957925811fb27.gif" title="\displaystyle{p(different) \approx e^{-(n^2 / 2 \cdot T)}}" alt="\displaystyle{p(different) \approx e^{-(n^2 / 2 \cdot T)}}"></p>

<p><img src="http://betterexplained.com/latexrender/pictures/7cacc718d713a80b55605d11f0ab203b.gif" title="\displaystyle{1 - p(match) \approx e^{-(n^2 / 2 \cdot T)}}" alt="\displaystyle{1 - p(match) \approx e^{-(n^2 / 2 \cdot T)}}"></p>

<p><img src="http://betterexplained.com/latexrender/pictures/b01d949a5fc5f6a0266a9c2ec4815b6d.gif" title="\displaystyle{1 - .5 \approx e^{-(n^2 / 2 \cdot T)}}" alt="\displaystyle{1 - .5 \approx e^{-(n^2 / 2 \cdot T)}}"></p>

<p><img src="http://betterexplained.com/latexrender/pictures/5c072f9fb83cf58116a6927be8398c51.gif" title="\displaystyle{-2ln(.5)\cdot T \approx n^2}" alt="\displaystyle{-2ln(.5)\cdot T \approx n^2}"></p>

<p><img src="http://betterexplained.com/latexrender/pictures/abd90df80b35f59813757a0757c7d5f8.gif" title="\displaystyle{n \approx 1.177 \sqrt{T}}" alt="\displaystyle{n \approx 1.177 \sqrt{T}}"></p>

<p>Voila! If you take sqrt(T) items (17% more if you want to be picky) then you have about a 50-50 chance of getting a match. If you <a href="http://tinyurl.com/2rhwwu">plug in other numbers</a> you can solve for other probabilities:</p>

<p><img src="http://betterexplained.com/latexrender/pictures/b080c3a0bbc55b4ce014037ea6c78958.gif" title="\displaystyle{n \approx \sqrt{-2ln(1-m)} \cdot \sqrt{T}}" alt="\displaystyle{n \approx \sqrt{-2ln(1-m)} \cdot \sqrt{T}}"></p>

<p>Remember that m is the <em>desired chance of a match</em> (it’s easy to get confused, I did it myself). If you want a 90% chance of matching birthdays, plug m=90% and T=365 into the equation and see that you need <a href="http://tinyurl.com/yw6fmx">41 people</a>.</p>

<p>Wikipedia has <a href="http://en.wikipedia.org/wiki/Birthday_paradox">even more details</a> to satisfy your inner nerd. Go forth and enjoy. </p></div>
</div>

<div class="feed">
  <div class="title">35. <a href="http://feeds.feedburner.com/~r/Betterexplained/~3/113419203/" target="_blank">An Intuitive Guide To Exponential Functions &amp; E</a></div>
  <div class="content"><p> <strong>e</strong> has always bothered me — not the letter, but the <strong>mathematical constant</strong>. What does it really mean?</p>

<p>Math books and even my beloved <a href="http://en.wikipedia.org/wiki/E_(mathematical_constant)" title="wikipedia">wikipedia</a> describe e using obtuse jargon:</p>

<blockquote>The mathematical constant e is the base of the natural logarithm.</blockquote>

<p>And when you look up <a href="http://en.wikipedia.org/wiki/Natural_logarithm" title="natural logarithm">natural logarithm</a> you get:</p>

<blockquote>The natural logarithm, formerly known as the hyperbolic logarithm, is the logarithm to the base e, where e is an irrational constant approximately equal to 2.718281828459.</blockquote>

<p>Nice circular reference there. It’s like a dictionary that defines labyrinthine with Byzantine:  it’s correct but not helpful. What’s wrong with everyday words like “complicated”?</p>

<p>I’m not picking on wikipedia — many math explanations are dry and formal in their quest for “rigor”. But this doesn’t help beginners trying to get a handle on a subject (and we were all a beginner at one point).</p>

<p>No more! Today I’m sharing my <strong>intuitive, high-level</strong> insights about what e is and why it rocks. Save your “rigorous” math book for another time.</p>

<h2>e is <span>NOT</span> Just a Number</h2>

<p>Describing e as “a constant approximately 2.71828…” is like calling <strong>pi</strong> “an irrational number, approximately equal to 3.1415…”. Sure, it’s true, but you completely missed the point.</p>

<p><strong>Pi is the ratio between circumference and diameter shared by all circles</strong>. It is a fundamental ratio inherent in all circles and therefore impacts any calculation of circumference, area, volume, and surface area for circles, spheres, cylinders, and so on. Pi is important and shows all circles are related, not to mention the trigonometric functions derived from circles (sin, cos, tan).</p>

<p><strong>e is the base amount of growth shared by all continually growing processes.</strong> E lets you take a simple growth rate (where all change happens at the end of the year) and find the impact of compound, continuous growth, where every nanosecond (or faster) you are growing just a little bit. </p>

<p>E shows up whenever systems grow exponentially and continuously: population, radioactive decay, interest calculations, and more. Even jagged systems that don’t grow smoothly can be <em>approximated</em> by e.</p>

<p>Just like every number can be considered a “scaled” version of 1 (the base unit), every circle can be considered a “scaled” version of the unit circle (radius 1), and every rate of growth can be considered a “scaled” version of e (the “unit” rate of growth).</p>

<p>So e is not an obscure, seemingly random number. <strong>e represents the idea that all continually growing systems are scaled versions of a common rate.</strong></p>

<h2>Understanding Exponential Growth</h2>

<p>Let start by looking at a basic system that <strong>doubles</strong> after an amount of time. For example,</p>


<ul>
<li>Bacteria can split and “doubles” every 24 hours</li>
<li>We get <a href="http://www.youtube.com/watch?v=auhHl5-6VdY">twice as many noodles</a> when we fold them in half.</li>
<li>Your money doubles every year if you get 100% return (lucky!)</li>
</ul>



<p>And it looks like this:</p>

<p><img src="http://betterexplained.com/wp-content/uploads/math/e/growth_2_x.png" alt="growth_2_x.png" title="growth_2_x.png" width="329" height="226" border="0"></p>

<p>Splitting in two or doubling is a very common progression. Sure, we can triple or quadruple, but doubling is convenient, so hang with me here.</p>

<p>Mathematically, if we have x splits then we get <code>2^x</code> times more “stuff” than when we started. With 1 split we have <code>2^1</code> or 2 times more. With 4 splits we have <code>2^4</code> = 16 times more. As a general formula:</p>

<p><img src="http://betterexplained.com/latexrender/pictures/5a4255067405db2b31d7dd98c96cc8b2.gif" title="\displaystyle{growth = 2^x}" alt="\displaystyle{growth = 2^x}"></p>

<p>Said another way, doubling is 100% growth. We can rewrite our formula like this:</p>

<p><img src="http://betterexplained.com/latexrender/pictures/b344c2cdf899baaf70fd40fea62712a8.gif" title="\displaystyle{growth = (1 + 100\%)^x}" alt="\displaystyle{growth = (1 + 100\%)^x}"></p>

<p>It’s the same equation, but we separate “2″ into what it really is: the original value (1) plus 100%. Clever, eh?</p>

<p>Of course, we can substitute 100% for any number (50%, 25%, 200%) and get the growth formula for that new rate. So the general formula for x periods of return is:</p>

<p><img src="http://betterexplained.com/latexrender/pictures/4e9bfa117d39e2bc2fe2139eec276a69.gif" title="\displaystyle{growth = (1 + return)^x}" alt="\displaystyle{growth = (1 + return)^x}"></p>

<p>This just means we multiply by our rate of return (1 + return) x times.</p>

<h2>A Closer Look</h2>

<p>Our formula assumes growth happens in discrete steps. Our bacteria are waiting, waiting, and then <strong>boom</strong>, they double at the very last minute. Our interest earnings magically appear at the 1 year mark. Based on the formula above, growth is punctuated and happens instantly. The green dots suddenly appear.</p>

<p>The world isn’t always like this. If we zoom in, we see that our bacterial friends split over time:</p>

<p><img src="http://betterexplained.com/wp-content/uploads/math/e/growth_2x_zoomed.png" alt="growth_2x_zoomed.png" title="growth_2x_zoomed.png" width="374" height="226" border="0"></p>

<p>Mr. Green doesn’t just show up: he slowly grows out of Mr. Blue. After 1 unit of time (24 hours in our case), Mr. Green is complete. He then becomes a mature blue cell and can create new green cells of his own.</p>

<p>Does this information change our equation?</p>

<p>Nope. In the bacteria case, the half-formed green cells still can’t do anything until they are fully grown and separated from their blue parents. The equation still holds.</p>

<h2>Money Changes Everything</h2>

<p>But money is different. As soon as we earn a penny of interest, that penny can start earning micro-pennies of its own. We don’t need to wait until we earn a complete dollar in interest — fresh money doesn’t need to mature.</p>

<p>Based on our <em>old formula</em>, interest growth looks like this:</p>

<p><img src="http://betterexplained.com/wp-content/uploads/math/e/growth_interest_1.png" alt="growth_interest_1.png" title="growth_interest_1.png" width="400" height="226" border="0"></p>

<p>But again, this isn’t quite right: all the interest appears on the last day. Let’s zoom in and split the year into two chunks. We earn 100% interest every year, or 50% every 6 months. So, we earn 50 cents the first 6 months and another 50 cents in the last half of the year:</p>

<p><img src="http://betterexplained.com/wp-content/uploads/math/e/growth_interest_6_months.png" alt="growth_interest_6_months.png" title="growth_interest_6_months.png" width="414" height="226" border="0"></p>

<p>But this <strong>still</strong> isn’t right! Sure, our original dollar (Mr. Blue) earns a dollar over the course of a year. But after 6 months we had a 50-cent piece, ready to go, that we neglected! That 50 cents could have earned money on its own:</p>

<p><img src="http://betterexplained.com/wp-content/uploads/math/e/growth_interest_6_months_compound.png" alt="growth_interest_6_months_compound.png" title="growth_interest_6_months_compound.png" width="414" height="226" border="0"></p>

<p>Because our rate is 50% per half year, that 50 cents would have earned 25 cents (50% times 50 cents). At the end of 1 year we’d have</p>


<ul>
<li>Our original dollar (Mr. Blue)</li>
<li>The dollar Mr. Blue made (Mr. Green)</li>
<li>The 25 cents Mr. Green made (Mr. Red)</li>
</ul>



<p>Giving us a total of $2.25. We gained $1.25 from our initial dollar, even better than doubling!</p>

<p>Now let’s turn our return into a formula. The growth of two half-periods of 50% is:</p>

<p><img src="http://betterexplained.com/latexrender/pictures/83df609c658681b2978a745aa1d29e11.gif" title="\displaystyle{growth = (1 + 100\%/2)^{2} = 2.25}" alt="\displaystyle{growth = (1 + 100\%/2)^{2} = 2.25}"></p>

<h2>Diving into Compound Growth</h2>

<p>Now let’s step it up a notch. Instead of splitting our growth into two periods of 50% growth, let’s split it into 3 segments of 33% growth. Who says we have to wait for 6 months before we start getting interest? Let’s get more granular in our counting.</p>

<p>Charting our growth for 3 compounded periods gives a funny picture:</p>

<p><img src="http://betterexplained.com/wp-content/uploads/math/e/growth_interest_4_months_compound.png" alt="growth_interest_4_months_compound.png" title="growth_interest_4_months_compound.png" width="414" height="226" border="0"></p>

<p>Think of each color as “shoveling” money upwards towards the other colors (its children), at 33% per period:</p>


<ul>
<li><strong>Month 0:</strong> We start with Mr. Blue at $1.</li>
<li><strong>Month 4:</strong> Mr. Blue has earned 1/3 dollar on himself, and creates Mr. Green, shoveling along 33 cents.</li>
<li><strong>Month 8:</strong> Mr. Blue earns another 33 cents and gives it to Mr. Green, bringing Mr. Green up to 66 cents. Mr. Green has actually earned 33% on his previous value, creating 11 cents (33% * 33 cents). This 11 cents becomes Mr. Red.</li>
<li><strong>Month 12:</strong> Things get a bit crazy. Mr. Blue earns another 33 cents and shovels it to Mr. Green, bringing Mr. Green to a full dollar. Mr. Green earns 33% return on his Month 8 value (66 cents), earning 22 cents. This 22 cents gets added to Mr. Red, who now totals 33 cents. And Mr. Red, who started at 11 cents, has earned 4 cents (33% * .11) on his own, creating Mr. Purple.</li>
</ul>



<p>Phew! The final value after 12 months is: 1 + 1 + .33 + .04 or about 2.37.</p>

<p>Take some time to really understand what’s happening with this growth:</p>


<ul>
<li>Each color earns interest on itself and “hands it off” to another color. The newly-created money can earn money of its own, and on the cycle goes.</li>
<li>I like to think of the original amount (Mr. Blue) as never changing. Mr. Blue shovels money to create Mr. Green, a steady 33 every 4 months since Mr. Blue does not change. In the diagram, Mr. Blue has a blue arrow showing how he feeds Mr. Green.</li>
<li>Mr. Green just happens to create and feed Mr. Red (green arrow), but Mr. Blue isn’t aware of this. </li>
<li>As Mr. Green grows over time (being constantly fed by Mr. Blue), he contributes more and more to Mr. Red. Between months 4-8 Mr. Green gives 11 cents to Mr. Red. Between months 8-12 Mr. Green gives 22 cents to Mr. Red, since Mr. Green was at 66 cents during Month 8. If we expanded the chart, Mr. Green would give 33 cents to Mr. Red, since Mr. Green reached a full dollar by Month 12.</li>
</ul>



<p>Make sense? It’s tough at first — I even confused myself a bit while putting the charts together. But see that each “dollar” creates little helpers, who in turn create helpers, and so on.</p>

<p>We get a formula by using 3 periods in our growth equation:</p>

<p><img src="http://betterexplained.com/latexrender/pictures/b5b4f6dccccca4550f1c9e2742166b9e.gif" title="\displaystyle{growth = (1 + 100\%/3)^3 = 2.37037...}" alt="\displaystyle{growth = (1 + 100\%/3)^3 = 2.37037...}"></p>

<p>We earned $1.37, even better than the $1.25 we got last time!</p>

<h2>Can We Get Infinite Money?</h2>

<p>Why not take even shorter time periods? How about every month, day, hour, or even nanosecond? Will our returns skyrocket?</p>

<p>Our return gets better, but only to a point. <a href="http://tinyurl.com/24ysoh">Try using different numbers of n</a> in our magic formula to see our total return:</p>



<pre>
<code>
n          (1 + 1/n)^n
------------------
1          2
2          2.25
3          2.37
5          2.488
10         2.5937
100        2.7048
1,000      2.7169
10,000     2.71814
100,000    2.718268
1,000,000  2.7182804
...
</code>
</pre>



<p>The numbers get bigger and converge around 2.718. Hey… wait a minute… that looks like e!</p>

<p>Yowza. In geeky math terms, e is <strong>defined</strong> to be that rate of growth if we continually compound 100% return on smaller and smaller time periods:</p>

<p><img src="http://betterexplained.com/latexrender/pictures/afab3e652841e07cc6d6f0cf52cb06a3.gif" title="\displaystyle{growth = e = \lim_{n\to\infty} \left( 1 + \frac{1}{n} \right)^n}" alt="\displaystyle{growth = e = \lim_{n\to\infty} \left( 1 + \frac{1}{n} \right)^n}"></p>

<p>This limit appears to converge, and there are proofs to that effect. But as you can see, as we take finer time periods the total return stays around 2.718. </p>

<h2>But what does it all mean?</h2>

<p>The number e (2.718…) represents the <strong>compound rate of growth</strong> from a process that grows at 100% for one time period.</p>

<p>If we start with $1.00 and compound continuously at 100% return we get 1e. If we start with $2.00, we get 2e. If we start with $11.79, we get 11.79e.</p>

<h2>What about different rates?</h2>

<p>Good question. What if we are grow at 50% annually, instead of 100%? Can we still use e?</p>

<p>Let’s see. The rate of 50% compound growth would look like this:</p>

<p><img src="http://betterexplained.com/latexrender/pictures/3be466175a45e5047b64ffa50fe298a6.gif" title="\displaystyle{\lim_{n\to\infty} \left( 1 + \frac{.50}{n} \right)^n}" alt="\displaystyle{\lim_{n\to\infty} \left( 1 + \frac{.50}{n} \right)^n}"></p>

<p>Hrm. What can we do here? Well, imagine we break it down into 50 chunks of 1% growth:</p>

<p><img src="http://betterexplained.com/latexrender/pictures/ea07cd7128aa65dd66239d231faead26.gif" title="\displaystyle{\left( 1 + \frac{.50}{50} \right)^{50} = \left( 1 + .01 \right)^{50}}" alt="\displaystyle{\left( 1 + \frac{.50}{50} \right)^{50} = \left( 1 + .01 \right)^{50}}"></p>

<p>Sure, it’s not infinity, but it’s pretty granular. Now imagine we broke down our “regular” rate of 100% into chunks of 1% growth as well:</p>

<p><img src="http://betterexplained.com/latexrender/pictures/f414c261380cae9ecc956bc4937017f5.gif" title="\displaystyle{e \approx \left( 1 + \frac{1.00}{100} \right)^{100} = \left( 1 + .01 \right)^{100}}" alt="\displaystyle{e \approx \left( 1 + \frac{1.00}{100} \right)^{100} = \left( 1 + .01 \right)^{100}}"></p>

<p>Ah, something is emerging here. In our regular case, we have 100 cumulative changes of 1% each. In the 50% scenario, we have 50 cumulative changes of 1% each.</p>

<p>What is the difference between the two numbers? Well, it’s just half the number of changes:</p>

<p><img src="http://betterexplained.com/latexrender/pictures/370b5ad5838e57d662f21f0ab33a5d5f.gif" title="\displaystyle{\left( 1 + .01 \right)^{50} = \left( 1 + .01 \right)^{100/2} = \left( \left( 1 + .01 \right)^{100}\right)^{1/2} = e^{1/2} }" alt="\displaystyle{\left( 1 + .01 \right)^{50} = \left( 1 + .01 \right)^{100/2} = \left( \left( 1 + .01 \right)^{100}\right)^{1/2} = e^{1/2} }"></p>

<p>This is pretty interesting. <span>50 / 100</span> = .5, which is the exponent we raise e to. This works in general: if we had a 300% growth rate, we could break it into 300 chunks of 1% growth. This would be triple the normal amount for a net rate of e^3.</p>

<p>Although we picked 1%, we could have chosen any small unit of growth (.1%, .0001%, or even an infinitely small amount!). The key is that for any rate we pick, it’s just a new exponent on e:</p>

<p><img src="http://betterexplained.com/latexrender/pictures/6b85332c7ee9191571619a2d0b71d7f7.gif" title="\displaystyle{growth = e^{rate}}" alt="\displaystyle{growth = e^{rate}}"></p>

<h2>What about different times?</h2>

<p>Suppose we have 300% growth for 2 years. We’d multiply one year’s growth (e^3) by itself two times:</p>

<p><img src="http://betterexplained.com/latexrender/pictures/8bffa8d81df36a066121e2ceb3fb475c.gif" title="\displaystyle{growth = \left(e^{3}\right)^{2} = e^{6}}" alt="\displaystyle{growth = \left(e^{3}\right)^{2} = e^{6}}"></p>

<p>And in general:</p>

<p><img src="http://betterexplained.com/latexrender/pictures/8eada761fb897e50177b9e78dd7aa835.gif" title="\displaystyle{growth = \left(e^{rate}\right)^{time} = e^{rate \cdot time}}" alt="\displaystyle{growth = \left(e^{rate}\right)^{time} = e^{rate \cdot time}}"></p>

<p>Because of the magic of exponents, we can avoid having two powers and just multiply rate and time together in a single exponent.</p>

<h2>The big secret: e merges rate and time.</h2>

<p>This is wild! e^x can mean two things:</p>


<ul>
<li>x is the number of times we multiply a growth rate: 100% growth for 3 years is e^3</li>
<li>x is the growth rate itself: 50% growth for one year is e^(.5).</li>
</ul>



<p>Won’t this overlap confuse things? Will our formulas break and the world come to an end?</p>

<p><strong>It all works out.</strong> When we write:</p>

<p><img src="http://betterexplained.com/latexrender/pictures/3a9a516b83e00876c6bc290d61751438.gif" title="\displaystyle{e^x}" alt="\displaystyle{e^x}"></p>

<p>the variable <strong>x</strong> is a combination of rate and time.</p>

<p><img src="http://betterexplained.com/latexrender/pictures/92085d1a16e47be2cf944dc63954b9d4.gif" title="\displaystyle{x = rate \cdot time}" alt="\displaystyle{x = rate \cdot time}"></p>

<p>Let me explain. In essence, 10 periods of 10% growth has the same growth impact as 1 period of 100% growth, when compounded:</p>


<ul>
<li>10 periods of 10% growth = 100 1% changes, happening over 10 years</li>
<li>1 period of 100% growth = 100 1% changes, happening over 1 year</li>
</ul>



<p><strong>The same number of changes happen in each case.</strong> Sure, the 100% growth happens <em>faster</em>. But the total change is the same! </p>

<p>After all is said and done, there were 100 periods of 1% growth, giving 2.718 growth in the end. One just happens to take more time than the other. Clearly 10 years of 100% growth is better than 10 years of 10% growth, but that wasn’t the question, now was it? <img src="http://betterexplained.com/wp-includes/images/smilies/icon_smile.gif" alt=":)"> </p>

<p>So, our general formula becomes:</p>

<p><img src="http://betterexplained.com/latexrender/pictures/0bfee4864a26c2188e106a5ad0d32557.gif" title="\displaystyle{growth = e^x = e^{rt}}" alt="\displaystyle{growth = e^x = e^{rt}}"></p>

<p>If we have a return of <strong>r</strong> for <strong>t</strong> time periods, our net compound growth is e^rt. This even works for negative and fractional returns, by the way.</p>

<h2>Example Time!</h2>

<p>Examples make everything more fun:</p>


<ul>
<li>I have 300 bacteria (don’t ask) that doubles every day. Assuming compound growth (let’s assume it), after 10 days we have 300 * e^(1 * 10) = 6.6 million of the little buggers.</li>
<li>I have $120 in an account bearing 5% interest. I keep it for 10 years. Assuming compound growth, I’d have 120 * e^(.05 * 10) = 197.85 after the 10 years.</li>
<li>I’ve got some radioactive material which decays at 50% every 3 months. I start with 20 pounds. How much will I have after 1 year? That’s 20 * e^(-.50 * 4) = 2.706 pounds. The “4″ is the number of periods of decay we have, which is 12 months / 3.</li>
</ul>



<h2>There’s More To Learn!</h2>

<p>My goal was to:</p>


<ul>
<li><strong>Explain why e is important:</strong> It’s a fundamental constant, like pi, that shows up in growth rates.</li>
<li><strong>Give an intuitive explanation:</strong> e lets you see the impact of any growth rate. Every new “piece” (Mr. Green, Mr. Red, etc.) helps add to the total growth.</li>
<li><strong>Show how it’s used:</strong> e^x lets you predict the impact of any growth rate and time period.</li>
<li><strong>Get you hungry for more:</strong> In the upcoming articles, I’ll dive into other properties of e.</li>
</ul>



<p>This article is just the start — cramming everything into a single page would tire you and me both. Dust yourself off, take a break and learn about e’s evil twin, the <a href="http://betterexplained.com/articles/demystifying-the-natural-logarithm-ln/">natural logarithm</a>. </p><img src="http://feeds.feedburner.com/~r/Betterexplained/~4/113419203" height="1" width="1"></div>
</div>

<div class="feed">
  <div class="title">36. <a href="http://feeds.feedburner.com/~r/Betterexplained/~3/114643256/" target="_blank">An Intuitive (and Short) Explanation of Bayes’ Theorem</a></div>
  <div class="content"><p> Bayes’ theorem was the subject of <a href="http://www.yudkowsky.net/bayes/bayes.html">a detailed article</a>. The essay is good, but over 15,000 words long — here’s the condensed version for Bayesian newcomers like myself:</p>


<ul>
<li><strong>Tests are not the event.</strong> We have a cancer <em>test</em>, separate from the event of actually having cancer. We have a <em>test</em> for spam, separate from the event of actually having a spam message.</li>
</ul>




<ul>
<li><strong>Tests are flawed.</strong> Tests detect things that don’t exist (false positive), and miss things that do exist (false negative).</li>
</ul>




<ul>
<li><strong>Tests give us test probabilities, not the real probabilities.</strong> People often consider the test results directly, without considering the errors in the tests.</li>
</ul>




<ul>
<li><strong>False positives skew results.</strong> Suppose you are searching for something really rare (1 in a million). Even with a good test, it’s likely that a positive result is really a <em>false positive</em> on somebody in the 999,999.</li>
</ul>




<ul>
<li><strong>People prefer natural numbers.</strong> Saying “100 in 10,000″ rather than “1%” helps people work through the numbers with fewer errors, especially with multiple percentages (”Of those 100, 80 will test positive” rather than “80% of the 1% will test positive”).</li>
</ul>




<ul>
<li><strong>Even science is a test</strong>. At a philosophical level, scientific experiments can be considered “potentially flawed tests” and need to be treated accordingly. There is a <em>test</em> for a chemical, or a phenomenon, and there is the <em>event</em> of the phenomenon itself. Our tests and measuring equipment have some inherent rate of error.</li>
</ul>



<p><strong>Bayes’ theorem gives you the actual probability of an event given the measured test probabilities.</strong> For example, you can:</p>


<ul>
<li><strong>Correct for measurement errors</strong>. If you know the real probabilities and the chance of a false positive and false negative, you can correct for measurement errors.</li>
</ul>




<ul>
<li><strong>Relate the actual probability to the measured test probability.</strong> Bayes’ theorem lets you relate Pr(A|X), the chance that an event A happened given the indicator X, and Pr(X|A), the chance the indicator X happened given that event A occurred. Given mammogram test results and known error rates, you can predict the actual chance of having cancer.</li>
</ul>



<h2>Anatomy of a Test</h2>

<p>The article describes a cancer testing scenario:</p>


<ul>
<li>1% of women have breast cancer (and therefore 99% do not).</li>
<li>80% of mammograms detect breast cancer when it is there (and therefore 20% miss it).</li>
<li>9.6% of mammograms detect breast cancer when it’s <strong>not</strong> there (and therefore 90.4% correctly return a negative result).</li>
</ul>



<p>Put in a table, the probabilities look like this:</p>

<p><img src="http://betterexplained.com/wp-content/uploads/math/bayes/bayes_table.png" alt="bayes_table.png" title="bayes_table.png" width="512" height="102" border="0"></p>

<p>How do we read it?</p>


<ul>
<li>1% of people have cancer</li>
<li>If you <strong>already have cancer</strong>, you are in the first column. There’s an 80% chance you will test positive. There’s a 20% chance you will test negative.</li>
<li>If you <strong>don’t have cancer</strong>, you are in the second column. There’s a 9.6% chance you will test positive, and a 90.4% chance you will test negative.</li>
</ul>



<h2>How Accurate Is The Test?</h2>

<p>Now suppose you get a positive test result. What are the chances you have cancer? 80%? 99%? 1%?</p>

<p>Here’s how I think about it:</p>


<ul>
<li>Ok, we got a positive result. It means we’re somewhere in the top row of our table. Let’s not assume anything — it could be a true positive or a false positive.</li>
<li>The chances of a <em>true positive</em> = chance you have cancer * chance test caught it = 1% * 80% = .008</li>
<li>The chances of a <em>false positive</em> = chance you don’t have cancer * chance test caught it anyway = 99% * 9.6% = 0.09504</li>
</ul>



<p>The table looks like this:</p>

<p><img src="http://betterexplained.com/wp-content/uploads/math/bayes/bayes_table_computed.png" alt="bayes_table_computed.png" title="bayes_table_computed.png" border="0"></p>

<p>And what was the question again? Oh yes: what’s the chance we really have cancer if we get a positive result. The chance of an event is the number of ways it could happen given all possible outcomes:</p>

<p><code>Probability = desired event / all possibilities</code></p>

<p>The chance of getting a real, positive result is .008. The chance of getting any type of positive result is the chance of a true positive plus the chance of a false positive (.008 + 0.09504 = .10304).</p>

<p>So, our chance of cancer is .008/.10304 = 0.0776, or about 7.8%.</p>

<p>Interesting — a positive mammogram only means you have a 7.8% chance of cancer, rather than 80% (the supposed accuracy of the test). It might seem strange at first but it makes sense: the test gives a false positive 10% of the time, so there will be a <strong>ton</strong> of false positives in any given population. There will be so many false positives, in fact, that <strong>most</strong> of the positive test results will be wrong.</p>

<p>If you take 100 people, only 1 person will have cancer. Another 10 will not have cancer but will get a false positive result. Getting a positive result means you only have a roughly 1/11 chance of being the person who really has cancer (7.8% to be exact).</p>

<h2>Bayes’ Theorem</h2>

<p>We can turn the process above into an equation, which is Bayes’ Theorem. It lets you take the test results and correct for the “skew” introduced by false positives. You get the real chance of having the event. Here’s the equation:</p>

<p><img src="http://betterexplained.com/latexrender/pictures/0f0bf0c4f3ce862116b1cda8dcda0156.gif" title="\displaystyle{\Pr(\mathrm{A}|\mathrm{X}) = \frac{\Pr(\mathrm{X}|\mathrm{A})\Pr(\mathrm{A})}{\Pr(\mathrm{X|A})\Pr(A)+ \Pr(\mathrm{X|\sim A})\Pr(\sim A)}&amp;#13;&amp;#10;}" alt="\displaystyle{\Pr(\mathrm{A}|\mathrm{X}) = \frac{\Pr(\mathrm{X}|\mathrm{A})\Pr(\mathrm{A})}{\Pr(\mathrm{X|A})\Pr(A)+ \Pr(\mathrm{X|\sim A})\Pr(\sim A)}&amp;#13;&amp;#10;}"></p>

<p>And here’s the decoder key to read it:</p>


<ul>
<li>Pr(A|X) = Chance of having cancer (A) given a positive test (X). This is what we want to know: How likely is it to have cancer with a positive result? In our case it was 7.8%.</li>
<li>Pr(X|A) = Chance of a positive test (X) given that you had cancer (A). This is the chance of a true positive, 80% in our case.</li>
<li>Pr(A) = Chance of having cancer (1%).</li>
<li>Pr(~A) = Chance of not having cancer (99%).</li>
<li>Pr(X|~A) = Chance of a positive test (X) given that you didn’t have cancer (~A). This is a false positive, 9.6% in our case.</li>
</ul>



<p>Try it with any number:</p>

<p></p>

<p>It all comes down to the chance of a <strong>true positive result</strong> divided by the <strong>chance of any positive result</strong>. We can simplify the equation to:</p>

<p><img src="http://betterexplained.com/latexrender/pictures/cefb028ea299aa6351578e636ab9ee9d.gif" title="\displaystyle{\Pr(\mathrm{A}|\mathrm{X}) = \frac{\Pr(\mathrm{X}|\mathrm{A})\Pr(\mathrm{A})}{\Pr(\mathrm{X})}}" alt="\displaystyle{\Pr(\mathrm{A}|\mathrm{X}) = \frac{\Pr(\mathrm{X}|\mathrm{A})\Pr(\mathrm{A})}{\Pr(\mathrm{X})}}"></p>

<p>Pr(X) is a normalizing constant and helps scale our equation. Without it, we might think that a positive test result gives us an 80% chance of having cancer.</p>

<p>Pr(X) tells us the chance of getting <em>any</em> positive result, whether it’s a real positive in the cancer population (1%) or a false positive in the non-cancer population (99%). It’s a bit like a weighted average, and helps us compare against the overall chance of a positive result.</p>

<p>In our case, Pr(X) gets really large because of the potential for false positives. Thank you, normalizing constant, for setting us straight! This is the part many of us may neglect, which makes the result of 7.8% counter-intuitive.</p>

<h2>Intuitive Understanding: Shine The Light</h2>

<p>The article mentions an intuitive understanding about shining a light through your real population and getting a test population. The analogy makes sense, but it takes a few thousand words to get there <img src="http://betterexplained.com/wp-includes/images/smilies/icon_smile.gif" alt=":)"> .</p>

<p>Consider a real population. You do some tests which “shines light” through that real population and creates some test results. If the light is completely accurate, the test probabilities and real probabilities match up. Everyone who tests positive is actually “positive”. Everyone who tests negative is actually “negative”.</p>

<p>But this is the real world. Tests go wrong. Sometimes the people who have cancer don’t show up in the tests, and the other way around. </p>

<p>Bayes’ Theorem lets us look at the skewed test results and correct for errors, recreating the original population and finding the real chance of a true positive result.</p>

<h2>Bayesian Spam Filtering</h2>

<p>One clever application of Bayes’ Theorem is in <a href="http://en.wikipedia.org/wiki/Bayesian_spam_filtering">spam filtering</a>. We have</p>


<ul>
<li>Event A: The message is spam.</li>
<li>Test X: The message contains certain words (X)</li>
</ul>



<p>Plugged into a more readable formula (from Wikipedia):</p>

<p><img src="http://betterexplained.com/latexrender/pictures/da59f0597e5b97578af81ba86fe6dc3e.gif" title="\displaystyle{\Pr(\mathrm{spam}|\mathrm{words}) = \frac{\Pr(\mathrm{words}|\mathrm{spam})\Pr(\mathrm{spam})}{\Pr(\mathrm{words})}}" alt="\displaystyle{\Pr(\mathrm{spam}|\mathrm{words}) = \frac{\Pr(\mathrm{words}|\mathrm{spam})\Pr(\mathrm{spam})}{\Pr(\mathrm{words})}}"></p>

<p>Bayesian filtering allows us to predict the chance a message is really spam given the “test results” (the presence of certain words). Clearly, words like “viagra” have a higher chance of appearing in spam messages than in normal ones.</p>

<p>Spam filtering based on a blacklist is flawed — it’s too restrictive and false positives are too great. But Bayesian filtering gives us a middle ground —  we use  <em>probabilities</em>. As we analyze the words in a message, we can compute the chance it is spam (rather than making a yes/no decision). If a message has a 99.9% chance of being spam, it probably is. As the filter gets trained with more and more messages, it updates the probabilities that certain words lead to spam messages. Advanced Bayesian filters can examine multiple words in a row, as another data point.</p>

<h2>Further Reading</h2>

<p>There’s a lot being said about Bayes:</p>


<ul>
<li><a href="http://en.wikipedia.org/wiki/Bayes%27s_theorem">Bayes’ Theorem on Wikipedia</a></li>
<li><a href="http://www.codinghorror.com/blog/archives/000850.html">Discussion on coding horror</a> </li>
<li><a href="http://www.yudkowsky.net/bayes/bayes.html">The big essay on Bayes’ Theorem</a></li>
</ul>



<p>Have fun! </p><img src="http://feeds.feedburner.com/~r/Betterexplained/~4/114643256" height="1" width="1"></div>
</div>

<div class="feed">
  <div class="title">37. <a href="http://feeds.feedburner.com/~r/Betterexplained/~3/116007358/" target="_blank">Astounding Examples of Innovation from Japan</a></div>
  <div class="content"><p> I traveled to Japan last year with some friends, and was <strong>astounded</strong> by the differences there. Everyday things from trash cans to doors to conveyerbelts were just… better, let alone the high-tech things like trains and computers.</p>

<p>I couldn’t help but compulsively photograph seemingly mundane things, awestruck by their design. I did get some quizzical looks when blabbering on about a straw or ladder. But there’s a few lessons:</p>


<ul>
<li>Unimaginative people ask “why?” when confronted with a new idea and sit there, lifeless. <strong>Instead, ask “why not?” and do something new.</strong></li>
<li>Not every invention pans out, and that’s fine. <strong>At least you are trying to improve things.</strong></li>
<li>Several minor, 2% improvements add up over time (see the <a href="http://betterexplained.com/articles/the-rule-of-72/">rule of 72</a>). Everyday efficiencies are great — you don’t always need a breakthrough to make a difference.</li>
</ul>



Browse the photos below and <a href="http://www.flickr.com/photos/kazad/sets/72157600205402468/detail/">read my comments at flickr</a> to see what the fuss was about. In upcoming posts I’ll expand on why these examples of innovation made me shriek with delight and <strong>what we can learn from them</strong>.<br>
<div><strong>You need to upgrade or install Adobe Flash Player</strong></div>

<p><br>
 </p><img src="http://feeds.feedburner.com/~r/Betterexplained/~4/116007358" height="1" width="1"></div>
</div>

<div class="feed">
  <div class="title">38. <a href="http://feeds.feedburner.com/~r/Betterexplained/~3/118316324/" target="_blank">Demystifying the Natural Logarithm (ln)</a></div>
  <div class="content"><p> After <a href="http://betterexplained.com/articles/an-intuitive-guide-to-exponential-functions-e/">understanding the exponential function</a> our next target is the natural logarithm.</p>

<p>Given how the natural log is described in math books, there’s little “natural” about it: it’s defined as the inverse of e^x, a strange enough exponent already.</p>

<p>But there’s a fresh, intuitive explanation: <strong>The natural log gives you the time needed to reach a certain amount of growth</strong>.</p>

<p>Let’s say you have an investment in gummy bears (and who doesn’t?) with <strong>simple interest</strong> of 100% per year. If you want 10x growth assuming continuous compounding, you’d wait only <a href="http://instacalc.com/?d=&amp;%23038;c=bG4oMTApfHx8fHx8&amp;%23038;s=sssssss&amp;%23038;v=0.9">ln(10) or 2.302 years</a>. Don’t see why it can more than double in a year? <a href="http://betterexplained.com/articles/an-intuitive-guide-to-exponential-functions-e/">Read more about e</a>.</p>

<p>The concepts are straightforward: </p>


<ul>
<li>e^x is the amount of continuous growth after a certain amount of time. </li>
<li>Natural Log (ln) is the amount of <strong>time</strong> needed to reach a certain level of growth.</li>
</ul>



<p>Not too bad, right? While the mathematicians scramble to give you the long, technical explanation, let’s dive into the intuitive one.</p>

<h2>E is about growth</h2>

<p>The number e is about continuous growth. <a href="http://betterexplained.com/articles/an-intuitive-guide-to-exponential-functions-e/">As we saw last time</a>, e^x lets us merge rate and time: 3 years at 100% growth is the same as 1 year at 300% growth, when continuously compounded.</p>

<p>We can take any combination of rate and time (50% for 4 years) and convert the rate to 100% for convenience (giving us 100% for 2 years). By converting to a rate of 100%, we only have time to think about:</p>

<p><img src="http://betterexplained.com/latexrender/pictures/5de0d736ed699c959542b5dfe8b6ee60.gif" title="\displaystyle{e^x = e^{rate \cdot time} = e^{1.0 \cdot time} = e^{time}}" alt="\displaystyle{e^x = e^{rate \cdot time} = e^{1.0 \cdot time} = e^{time}}"></p>

<p>Intuitively, e^x means:</p>


<ul>
<li>How much growth do I get after after x units of time (and 100% growth)</li>
<li>For example: after 3 time periods I have e^3 = 20.08 times the amount of “stuff”.</li>
</ul>



<p>e^x is a scaling factor, showing us how much growth we’d get after x units of time.</p>

<h2>Natural Log is about time</h2>

<p>The natural log is the inverse of e, a fancy term for opposite. Speaking of fancy, the Latin name is <em>logarithmus naturali</em>, giving the abbreviation <em>ln</em>.</p>

<p>Now what does this inverse or opposite stuff mean?</p>


<ul>
<li>e^x lets us plug in <strong>time</strong> and get growth.</li>
<li>ln(x) lets us plug in <strong>growth</strong> and get the <strong>time it would take.</strong></li>
</ul>



<p>For example:</p>


<ul>
<li>e^3 is 20.08. After 3 units of time, we end up with 20.08 more than we started with.</li>
<li>ln(20.08) is about 3. If we want growth of 20.08, we’d wait 3 units of time (at 100% growth).</li>
</ul>



<p>With me? The natural log gives us the time needed to hit our desired growth (assuming 100% growth rate).</p>

<h2>Logarithmic Arithmetic Is Not Normal</h2>

<p>You’ve studied logs before, and they were strange beasts. How’d they turn multiplication into addition? Division into subtraction? Let’s see.</p>

<p>What is ln(1)? Intuitively, the question is: How long do I wait to get 1x my current amount?</p>

<p>Zero. Zip. Nada. You’re already <strong>at</strong> 1x your current amount! It doesn’t take any time to grow from 1 to 1.</p>


<ul>
<li>ln(1) = 0.</li>
</ul>



<p>Ok, how about a fractional value? How long to get 1/2 my current amount? Assuming you are growing at 100%, we know that ln(2) is the amount of time to double. If we <strong>reverse it</strong> (i.e., take the negative time) we’d have half of our current value.</p>


<ul>
<li>ln(.5) = - ln(2) = -.693</li>
</ul>



<p>Makes sense, right? If we go backwards (negative time) .693 seconds we’d have half our current amount. In general, you can flip the fraction and take the negative: ln(1/3) = - ln(3) = -1.09. This means if we go back 1.09 units of time, we’d have a third of what we have now.</p>

<p>Ok, how about the natural log of a negative number? How much time does it take to “grow” your bacteria colony from 1 to -3?</p>

<p>It’s impossible! You can’t have a “negative” amount of bacteria, can you? At most (er… least) you can have zero, but there’s no way to have a negative amount of the little critters. Negative bacteria just doesn’t make sense.</p>


<ul>
<li>ln(negative number) = undefined</li>
</ul>



<p>Undefined just means “there is no amount of time you can wait” to get a negative amount.</p>

<h2>Logarithmic Multiplication is Mighty Fun</h2>

<p>How long does it take to grow 4x your current amount? Yeah, we could just use ln(4). But that’s too easy, let’s be different.</p>

<p>We can consider 4x growth as doubling (taking ln(2) units of time) and then doubling again (taking another ln(2) units of time):</p>


<ul>
<li>Time to grow 4x = ln(4) = Time to double and double again = ln(2) + ln(2)</li>
</ul>



<p>Interesting. Any growth number, like 20, can be considered 2x growth followed by 10x growth. Or 4x growth followed by 5x growth. Or 3x growth followed by 6.666x growth. See the pattern?</p>


<ul>
<li>ln(a*b) = ln(a) + ln(b)</li>
</ul>



<p>The log of a times b = log a + log b. This relationship <strong>makes sense</strong> when you think about it being the time to grow.</p>

<p>If we want to grow 30x, we can wait ln(30) all at once, or simply wait ln(3), to triple, then ln(10), to grow 10x again. The net effect is the same, so the net time should be the same too (and it is).</p>

<p>How about division? ln(5/3) is saying: How long does it take to grow 5 times and then take 1/3 of that?</p>

<p>Well, growing 5 times is ln(5). Growing 1/3 is -ln(3) units of time. So</p>


<ul>
<li>ln(5/3) = ln(5) - ln(3)</li>
</ul>



<p>Which means: Grow 5 times and “go back in time” until you have a third of that amount. In general we have</p>


<ul>
<li>ln(a/b) = ln(a) - ln(b)</li>
</ul>



<p>I hope the strange math of logarithms is starting to make sense: multiplication of growth becomes addition of time, division of growth becomes subtraction of time. Don’t memorize the rules, <strong>understand them</strong>.</p>

<h2>Using Natural Logs With Any Rate</h2>

<p>“Sure,” you say, “This log stuff works for 100% growth but what about the 5% I normally get?”</p>

<p>It’s no problem. The “time” we get back from ln() is actually a combination of rate and time, the “x” from our e^x equation. We just assume 100% to make it simple, but we can use other numbers.</p>

<p>Suppose we want 30x growth: plug in ln(30) and get 3.4. This means:</p>


<ul>
<li>e^x = growth</li>
<li>e^3.4 = 30</li>
</ul>



<p>And intuitively this equation means “100% return for 3.4 years is 30x growth”. We can consider the equation to be:</p>

<p><img src="http://betterexplained.com/latexrender/pictures/b860df41a2714e4593863898eaf6b57f.gif" title="\displaystyle{e^{x} = e^{rate \cdot time}}" alt="\displaystyle{e^{x} = e^{rate \cdot time}}"></p>

<p><img src="http://betterexplained.com/latexrender/pictures/e0c9c3cd2a90f0f666f4d41b052dd91e.gif" title="\displaystyle{e^{100 \% \cdot 3.4 years} = 30}" alt="\displaystyle{e^{100 \% \cdot 3.4 years} = 30}"></p>

<p>We can modify “rate” and “time”, as long rate * time = 3.4. For example:</p>


<ul>
<li>3.4 years at 100% = 3.4 * 1.0 = 3.4</li>
<li>1.7 years at 200% = 1.7 * 2.0 = 3.4</li>
<li>6.8 years at 50% = 6.8 * 0.5 = 3.4</li>
<li>68 years at 5% = 68 * .05 = 3.4</li>
</ul>



<p>Cool, eh? The natural log can be used with any <strong>interest rate or time</strong> as long as their product is the same. You can wiggle the variables all you want.</p>

<h2>Awesome example: The Rule of 72</h2>

<p>The <a href="http://betterexplained.com/articles/the-rule-of-72/">Rule of 72</a> is a <a href="http://betterexplained.com/articles/mental-math-shortcuts/">mental math shortcut</a> to estimate the time needed to double your money. We’re going to derive it (yay!) and even better, we’re going to understand it intuitively.</p>

<p>How long does it take to double your money at 100% simple interest?</p>

<p>This should now be an easy question: ln(2) = .693. It takes .693 units of time (years, let’s say) to double your money with continuous compounding with a rate of 100% simple interest.</p>

<p>Ok, what if our interest isn’t 100% What if it’s 5% or 10%?</p>

<p>Simple. As long as rate * time = .693, we’ll double our money:</p>


<ul>
<li>rate * time = .693</li>
<li>time = .693/rate</li>
</ul>



<p>So, if we only had 10% growth, it’d take .693 / 10% or 6.93 years to double.</p>

<p>To simplify things, let’s multiply by 100 so we can talk about 10 rather than .10:</p>


<ul>
<li>time to double = 69.3/rate, where rate is assumed to be in percent.</li>
</ul>



<p>Now the time to double at 5% growth is 69.3/5 or 13.86 years. However, 69.3 isn’t the most divisible number. Let’s pick a close neighbor, 72, which can be divided by 2, 3, 4, 6, 8 and many more numbers.</p>


<ul>
<li>time to double = 72/rate</li>
</ul>



<p>which is the rule of 72! Easy breezy.</p>

<p>If you want to find the time to triple, you’d use ln(3) ~ 109.8 and get</p>


<ul>
<li>time to triple = 110 / rate</li>
</ul>



<p>Which is another useful rule of thumb. The Rule of 72 is useful for interest rates, population growth, bacteria cultures, and anything that grows exponentially.</p>

<p>One caveat: notice how we’re converting between simple and compound interest - won’t this mess up our formula? Yes, it does, but at <strong>reasonable interest rates</strong> like 5%, 6% or even 15%, there isn’t much difference between simple and compound interest. So the rough formula works, uh, roughly.</p>

<h2>Where to from here?</h2>

<p>I hope the natural log makes more sense — it tells you the <strong>time</strong> needed for any amount of exponential growth. I consider it “natural” because e is the universal rate of growth, so ln could be considered the “universal” way to figure out how long things take to grow.</p>

<p>When you see ln(x), just think “the amount of time to grow to x”. In an upcoming article I’ll bring e and ln together, and the sweet aroma of math will fill the air.</p>

<h2>Appendix: The Natural Log of E</h2>

<p>Quick quiz: What’s ln(e)?</p>


<ul>
<li>The math robot says: Because they are defined to be inverse functions, clearly ln(e) = 1</li>
<li>The intuitive human: ln(e) is the amount of time it takes to get “e” units of growth (about 2.718). But e <strong>is</strong> the amount of growth after <strong>1 unit of time</strong>, so ln(e) = 1.</li>
</ul>



<p>Think intuitively. </p><img src="http://feeds.feedburner.com/~r/Betterexplained/~4/118316324" height="1" width="1"></div>
</div>

<div class="feed">
  <div class="title">39. <a href="http://feeds.feedburner.com/~r/Betterexplained/~3/125471394/" target="_blank">Starting Ruby on Rails: What I Wish I Knew</a></div>
  <div class="content"><p> <a href="http://rubyonrails.com">Ruby on Rails</a> is an elegant, compact and fun way to build web applications. Unfortunately, many gotchas await the new programmer. Now that I have a few <a href="http://instacalc.com">rails projects</a> under my belt, here’s my shot at sparing you the suffering I experienced when first getting started.</p>

<h2>Tools: Just Get Them</h2>

<p>Here’s the tools you’ll need. Don’t read endless reviews trying to decide on the best one; start somewhere and get going.</p>


<ul>
<li><a href="http://www.pragmaticprogrammer.com/titles/rails/">Agile Web Development with Rails</a>. Yes, it’s a book. And a cohesive book is worth 100 hobbled-together <a href="http://wiki.rubyonrails.com/rails">online tutorials</a>.</li>
<li><a href="http://rubyforge.org/projects/instantrails/">InstantRails</a>: A .zip file containing Ruby, Apache, MySQL and <span>PHP </span>(for PhpMyAdmin), packaged and ready to go.</li>
<li><a href="http://www.aptana.com">Aptana/RadRails</a> (like Eclipse) or <a href="http://www.sapphiresteel.com/">Ruby In Steel</a> (like Visual Studio) for editing code.</li>
<li><a href="http://subversion.tigris.org" title="command-line">Subversion</a> and/or <a href="http://tortoisesvn.net/downloads" title="Windows GUI">TortoiseSVN</a> for source control.</li>
<li>Browse popular ruby on rails links on <a href="http://del.icio.us/popular/rubyonrails">del.icio.us</a>, <a href="http://railsbrain.com">Rails documentation</a> and <a href="http://docs.huihoo.com/ruby/ruby-man-1.4/syntax.html">Ruby syntax &amp; examples</a>.</li>
</ul>



<h2>But What Does It All Mean?</h2>

<p>“Ruby on Rails” is catchy but confusing. Is Rails some type of magical drug that Ruby is on? (Depending on who you ask, yes.)</p>

<p><strong>Ruby</strong> is a programming language, similar to Python and Perl. It is dynamically typed (no need for “int i”), interpreted, and can be modified at runtime (such as adding new methods to classes). It has dozens of shortcuts that make it very clean; methods are rarely over 10 lines. It has good RegEx support and works well for shell scripting.</p>

<p><strong>Rails</strong> is a <a href="http://rubygems.org/read/book/1">gem</a>, or a Ruby library. Some gems let you use the Win32 <span>API.</span> Others handle networking. Rails helps make web applications, providing classes for saving to the database, handling <span>URL</span>s and displaying html (along with a webserver, maintenance tasks, a debugging console and much more).</p>

<p><strong><span>IRB</span></strong> is the interactive Ruby console (type “irb” to use). Rails has a special <span>IRB </span>console to access your web app as it is running (excellent for live debugging).</p>

<p><strong>Rake</strong> is Ruby’s version of Make. Define and run maintenance tasks like setting up databases, reloading data, backing up, or even deploying an app to your website.</p>

<p><strong>Erb</strong> is embedded Ruby, which is like <span>PHP.</span> It lets you mix Ruby with <span>HTML </span>(for example):</p>



<pre>
<code>&lt;div&gt;Hello there, &lt;%= get_user_name() %&gt;&lt;/div&gt;</code>
</pre>



<p><strong><span>YAML </span>(or <span>YML</span>)</strong> is a “Yet Another Markup Language”, a simple way to <a href="http://en.wikipedia.org/wiki/YAML">specify data</a>:</p>



<pre>
<code>{name: John Smith, age: 33}</code>
</pre>



<p>It’s like <span>JSON, </span>much leaner than <span>XML, </span>and used by Rails for setting configuration options (like setting the database name and password).</p>

<p>Phew! Once Ruby is installed and in your path, you can add the rails gem using:</p>



<pre>
<code>
gem install rails
</code>
</pre>



<p>In general, use gem install “gem_name”, which searches online sources for that library. Although Rails is “just another gem”, it is the killer library that brought Ruby into the limelight.</p>

<h2>Understanding Ruby-Isms</h2>

<p>It’s daunting to learn a new library and a new language at the same time. Here are some of the biggest Ruby gotchas for those with a C/C++/Java background.</p>

<p><strong>Ruby removes unnecessary cruft: (){};</strong></p>


<ul>
<li>Parenthesis on method calls are optional; use <code>print "hi"</code>. </li>
<li>Semicolons aren’t needed after each line (crazy, I know). </li>
<li>Use “if then else end” rather than braces. </li>
<li>Parens aren’t needed around the conditions in if-then statements.</li>
<li>Methods automatically return the last line (call <code>return</code> explicitly if needed)</li>
</ul>



<p>Ruby scraps the annoying, ubiquitous punctuation that distracts from the program logic. Why put parens ((around),(everything))? Again, if you want parens, put ‘em in there. But you’ll take off the training wheels soon enough.</p>

<p>The line noise (er, “punctuation”) we use in C and Java is for the compiler’s benefit, not ours. Be warned: after weeks with Ruby, other languages become a bit painful to read.</p>



<pre>
<code>
def greet(name)              # simple method
   &quot;Hello, &quot; + name          # returned automatically
end

greet &quot;world&quot;                # ==&gt; &quot;Hello, world&quot;
</code>
</pre>



<p><strong>Those Funny Ruby Variables</strong></p>


<ul>
<li><code>x = 3</code> is a local variable for a method or block (gone when the method is done)</li>
<li><code>@x = 3</code> is a instance variable owned by each object (it sticks around)</li>
<li><code>@@x = 3</code> is a class variable shared by all objects (it sticks around, too).</li>
<li><code>:hello</code> is a symbol, like a constant string. Useful for indexing hashes. Speaking of which…</li>
<li><code>dictionary = { :cat =&gt; &quot;Goes meow&quot;, :dog =&gt; &quot;Barks loud.&quot;}</code> is a hash of key/value pairs. Access elements with dictionary[:cat].</li>
</ul>



<p><strong>Those Funny Ruby Assignments</strong></p>

<p>Ruby has the <code>||</code> operator which is a bit funky. When put in a chain</p>



<pre>
<code>x = a || b || c || "default"</code> 
</pre>



<p>it means “test each value and return the first that’s not false.” So if a is false, it tries b. If b is false, it tries c. Otherwise, it returns the string “default”.</p>

<p>If you write <code>x = x || "default"</code> it means “set x to itself (if it has a value), otherwise use the default.” An easier way to write this is</p>

<p><code>x ||= "default"</code></p>

<p>which means the same: set x to the default value unless it has some other value. You’ll see this a lot in Ruby programs.</p>

<p><strong>Those Funny Ruby Blocks</strong></p>

<p>Ruby has “blocks”, which are like anonymous functions passed to a loop or another function. These blocks can specify a parameter using |param| and then take actions, call functions of their own, and so on. Blocks are useful when applying some function to each element of an array. It helps to think of them as a type of anonymous function that can, but doesn’t have to, take a parameter.</p>



<pre>
<code>
3.times do |i|
   print i*i
end
</code>
</pre>



<p>In this example, the numbers 0,1 and 2 are passed to a block (do… end) that takes a single parameter (i) and prints i squared. The output would be 0, followed by 1 followed by 4 (and looks like “014″ since we didn’t include spaces). Blocks are common in Ruby but take some getting used to, so be forewarned.</p>

<p>These are the Ruby lessons that were tricky when starting out. Try <a href="http://qa.poignantguide.net/">Why’s Poignant Guide To Ruby</a> for more info (”Why” is the name of the author… it confused me too).</p>

<h2>Understanding Rails-isms</h2>

<p>Rails has its own peculiarities. “Trust us, it’s good for you.” say the programmers. It’s true - the features/quirks make Rails stand out, but they’re confusing until they click. Remember:</p>


<ul>
<li><strong>Class and table names are important.</strong> Rails has certain naming conventions; it expects objects from the class <code>Person</code> to be saved to a database table named <code>people</code>. Yes, Rails has a <strong>pluralization engine</strong> to figure out what object maps to what table (<a href="http://nubyonrails.com/tools/pluralize">I kid you not</a>).  This magic is great, but scary at first when you’re not sure how classes and tables are getting linked together.</li>
<li>Many methods take an “options” hash as a parameter, rather than having dozens of individual parameters. When you see</li>
</ul>





<pre>
<code>
link_to &quot;View Post&quot;, :action =&gt; &#39;show&#39;, :controller =&gt; &#39;article&#39;, :id =&gt; @article
</code>
</pre>



<p>The call is really doing this:</p>



<pre>
<code>
link_to(&quot;View Post&quot;, {:action =&gt; &#39;show&#39;, :controller =&gt; &#39;article&#39;, :id =&gt; @article})
</code>
</pre>



<p>There are only two parameters: the name (”View Post”) and a hash with 3 key/value pairs. Ruby lets us remove the extra parens and braces, leaving the stripped-down function call above.</p>

<h2>Understanding The Model-View-Controller Pattern</h2>

<p>Rails is built around the <a href="http://slash7.com/articles/2005/2/22/mvc-the-most-vexing-conundrum">model-view-controller</a> pattern. It’s a simple concept: separate the data, logic, and display layers of your program. This lets you split functionality cleanly, just like having separate <span>HTML, CSS </span>and Javascript files prevents your code from mushing together. Here’s the <span>MVC </span>breakdown:</p>


<ul>
<li><strong>Models</strong> are classes that talk to the databse. You find, create and save models, so you don’t (usually) have to write <span>SQL.</span> Rails has a class to handle the magic of saving to a database when a model is updated.</li>
<li><strong>Controllers</strong> take user input (like a <span>URL</span>) and decide what to do (show a page, order an item, post a comment). They may initially have business logic, like finding the right models or changing data. As your rails ninjitsu improves, constantly refactor and move business logic into the model (<a href="http://weblog.jamisbuck.org/2006/10/18/skinny-controller-fat-model">fat model, skinny controller</a>). Ideally, controllers just take inputs, call model methods, and pass outputs to the view (including error messages).</li>
<li><strong>Views</strong> display the output, usually <span>HTML.</span> They use <span>ERB </span>and this part of Rails is like <span>PHP </span>- you use <span>HTML </span>templates with some Ruby variables thrown in. Rails also makes it easy to create views as <span>XML </span>(for web services/RSS feeds) or <span>JSON </span>(for <span>AJAX </span>calls).</li>
</ul>



<p>The <span>MVC </span>pattern is key to building a readable, maintainable and easily-updateable web app.</p>

<h2>Understanding Rails’ Directory Structure</h2>

<p>When you create your first rails app, the directories are laid out for you. The structure is well-organized: Models are in app/models, controllers in app/controllers, and views in app/my_local_views (just kidding).</p>

<p>The naming conventions are important - it lets rails applications “find their parts” easily, without additional configuration. Also, it’s very easy for another programmer to understand and learn from any rails app. I can take a look at Typo, the rails blogging software, and have a good idea of how it works in minutes. Consistency creates comprehension.</p>

<h2>Understanding Rails’ Scaffolding</h2>

<p>Scaffolding gives you default controller actions (URLs to visit) and a view (forms to fill out) to interact with your data — you don’t need to build an interface yourself. You do need to define the Model and create a database table.</p>

<p>Think of scaffolds as the “default” interface you can use to interact with your app - you’ll slowly override parts of the default as your app is built. You specify scaffolds in the controller with a single line:</p>



<pre><code>scaffold :person</code></pre>



<p>and it adds default actions and views for showing, editing, and creating your “Person” object. Rails forms take some getting used to, so scaffolding helps a lot in the initial stages.</p>

<h2>More Tips and Tricks</h2>

<p>I originally planned on a list of tips &amp; tricks I found helpful when learning rails. It quickly struck me that Ruby on Rails actually requires a lot of background knowledge, and despite (or because of) its “magic”, it can still be confusing. I’ll get into my favorite tricks in an upcoming article.</p>

<p>As you dive further into web development, these guides may be helpful:</p>


<ul>
<li><a href="http://betterexplained.com/articles/how-to-debug-web-applications-with-firefox/">How To Debug Web Applications With Firefox</a></li>
<li><a href="http://betterexplained.com/articles/how-to-optimize-your-site-with-http-caching/">How To Optimize Your Site With <span>HTTP</span> Caching</a></li>
<li><a href="http://betterexplained.com/articles/how-to-optimize-your-site-with-gzip-compression/">How To Optimize Your Site With <span>GZIP</span> Compression</a></li>
<li><a href="http://betterexplained.com/articles/speed-up-your-javascript-load-time/">Speed Up Your Javascript Load Time</a></li>
<li><a href="http://betterexplained.com/articles/the-quick-guide-to-guids/">The Quick Guide to <span>GUID</span>s</a></li>
</ul>



<p>Until next time, enjoy these amusing videos:</p>

<p><embed src="http://www.youtube.com/v/p5EIrSM8dCA" width="425" height="350" allowScriptAccess="never" wmode="transparent" type="application/x-shockwave-flash"></embed></p>

<p><embed src="http://www.youtube.com/v/H868NSM2yAg" width="425" height="350" allowScriptAccess="never" wmode="transparent" type="application/x-shockwave-flash"></embed> </p><img src="http://feeds.feedburner.com/~r/Betterexplained/~4/125471394" height="1" width="1"></div>
</div>

<div class="feed">
  <div class="title">40. <a href="http://feeds.feedburner.com/~r/Betterexplained/~3/140756929/" target="_blank">Build a site you (and your readers) will love</a></div>
  <div class="content"><p> I’m thrilled by the <a href="http://digg.com/programming/Starting_Ruby_on_Rails_What_I_Wish_I_Knew">recent attention</a> and <a href="http://www.stumbleupon.com/url/betterexplained.com/articles/mental-math-shortcuts/">your feedback</a>. Seeing your “Aha!” moments motivates me to write — though I’ve been neglectful lately. I’m busy, the dog ate my browser, the draft’s in the email… you know the drill. If you need an explanation fix, check out my old site from college:</p>

<p><a href="http://www.cs.princeton.edu/~kazad/resources.htm">http://www.cs.princeton.edu/~kazad/resources.htm</a></p>

<p>It has many posts I’ll be revising and importing over time.</p>

<p>This post is for betterexplained newcomers and old-timers: the how and why of the site. If you like my approach, it’s a guide to writing. Otherwise it’s your own list of advice to avoid.</p>

<h2>Find your purpose</h2>

<p>Blogging is introspective. I’ve realized my interests <a href="http://betterexplained.com/articles/surviving-and-thriving-on-your-own-know-thyself/">after much thought</a>:</p>


<ul>
<li>I like math, writing, computers, business, personal development, communication, and learning.</li>
<li><strong>I cringe when ideas are explained poorly.</strong> Jargon and complicated explanations discourage the beginner. It shows you don’t really know the material. I get upset thinking that a poor explanation may turn someone away from a field forever, and want to fix that.</li>
<li><strong>I have many beliefs about education</strong>. Insight beats memorization. Any subject (anything!) can be explained simply if you understand it well enough. Curiosity and passion are enough to conquer a subject.</li>
</ul>



I’ve always wanted to share hard-won ideas and save other people mental anguish — BetterExplained has been a nagging thought in my mind. Today it’s alive with a purpose:<br>
<blockquote>
To explain topics clearly, intuitively, and share the “a-ha!” moments that make learning fun. Any subject can be better explained; today it’s just me writing, but I want to catalog insights from everyone.<br>
</blockquote>

<p>This vision excites me — find the one that motivates you.</p>

<h2>Be yourself (it’s harder than you think)</h2>

<p>Writing naturally is hard. When you put pen to paper, fingers to keyboard, or stand before an audience, you stiffen. You get self conscious. You don’t act like yourself.</p>

<p>Fortunately this feeling <a href="http://instacalc.com/blog/instacalc-in-the-real-world">disappears with practice</a>. You stop pontificating; you explain. You don’t “write an article”; you have a conversation. You use humor, stories, and personal examples instead of abstract generalities. You write even if people won’t agree with everything you say.</p>

<p>Use your talents in whatever combination you can.</p>

<blockquote>
But if you want something extraordinary, you have two paths:

<p>1. Become the best at one specific thing.<br>
2. Become very good (top 25%) at two or more things.</p>

– <a href="http://dilbertblog.typepad.com/the_dilbert_blog/2007/07/career-advice.html">Scott Adams</a><br>
</blockquote>

<p>I feel my greatest talents are being curious, having enthusiasm and wanting things to be simple. I’m no expert. But I’m going to learn what I can and share it in the most intuitive way possible. Someone I admire feels similarly:</p>

<blockquote>
I have no special talents. I am only passionately curious.<br>
–Albert Einstein<br>
</blockquote>

<h2>Write what you know; link what you don’t</h2>

<p>You may not be an expert (I’m not), but you’ve collected nuggets of information and personal insights that nobody else has. Share them in your own style.</p>

<p>Then link to wikipedia, delicious, and the detailed articles so readers can learn the nitty gritty. Eventually you’ll learn the nitty gritty too, have your own insights, and simplify them into a new article.</p>

<h2>Write well</h2>

<p>Writing well is hard. Really hard. My definition means:</p>


<ul>
<li><strong>Timeless content</strong> that is still relevant in a year.</li>
<li><strong>Original thought and deep insights</strong> that aren’t immediately obvious. If sharing details, organize them in a clever way.</li>
<li><strong>Fun to read</strong>. Write for people. Use humor, quotes, stories. Remember: I’m Kalid, you’re you, and we’re having a conversation. This is no textbook.</li>
<li><strong>Succinct, clear, and organized.</strong> I want to pump ideas into your head as fast as possible (I hope you don’t mind). Fewer words = faster intake = happier user.</li>
</ul>



<p>That’s the goal, not that I always reach it.</p>

<p>Why write quality, not quantity? In my experience, an outstanding post trumps a dozen average ones. Top posts create traffic, links, diggs, and get people talking. You receive emails and comments which make your day and motivate you to write more, and better. Sub-par posts dilute your site and waste time.</p>

<p>Astound visitors with the quality of your content. Define your own quality bar and run towards it. Be merciless when revising. Don’t be afraid to fix up old posts — streamlining previous articles is good practice.</p>

<h2>Just write</h2>

<p>Blogging has no holy book. This is a non-fiction site about math and programming topics; a poetry blog has different goals. I don’t know your goals, so just write and push your own bar. You can be top-quality in your field.</p>


<p>I think I have above-average interest in math, science, simplicity, education, curiosity, and passion. The combination lead to my style and the focus of this site. I’m thrilled that others seem to like it too. Find your style; someone will like it.</p>

<h2>Pace yourself</h2>

<p>Writing on a consistent schedule is hard — if you’ve mastered the secret, let me know. I have dozens of posts in various draft forms, and it seems I need a Herculean effort to go back and revise them. Some posts stream out of my head and I’m done in a few hours. Others weigh over me for days or weeks, requiring a flurry of energy to finish and clean up.</p>

<p>I suggest an “articles” folder to collect your thoughts, in whatever stage. Sometimes you just have a sentence or two, but it can grow into a whole post over time.</p>

<p>I’ve learned writing isn’t all fun, even on topics you enjoy. Editing can be painful — push yourself through.</p>

<h2>Keep learning</h2>

<p>My opinions were shaped by these authors:</p>


<ul>
<li><a href="http://steve.yegge.googlepages.com/you-should-write-blogs">Steve Yegge</a></li>
<li><a href="http://www.problogger.net/archives/2007/07/13/how-to-write-excellent-blog-content-what-we-wish-we-knew/">Darren Rowse</a></li>
<li><a href="http://www.paulgraham.com/writing44.html">Paul Graham</a></li>
<li><a href="http://www.stevepavlina.com/blog/2006/01/how-to-build-a-high-traffic-web-site-or-blog/">Steve Pavlina</a></li>
<li><a href="http://www.writingclasses.com/InformationPages/index.php/PageID/300">George Orwell’s Tips</a></li>
<li><a href="http://www.amazon.com/Elements-Style-Fourth-William-Strunk/dp/020530902X">Elements of Style</a> (<a href="http://www.crockford.com/wrrrld/style.html">read online</a>) by Strunk and White</li>
<li><a href="http://www.amazon.com/Writing-Well-25th-Anniversary-Nonfiction/dp/0060006641">On Writing Well</a> by White’s mentee, William Zinsser.</li>
</ul>



<p>Good luck in your writing. Now that you know my passions, I’m interested in what <strong>you</strong> care about. Drop me a note anytime.<br>
 </p><img src="http://feeds.feedburner.com/~r/Betterexplained/~4/140756929" height="1" width="1"></div>
</div>

<div class="feed">
  <div class="title">41. <a href="http://feeds.feedburner.com/~r/Betterexplained/~3/143279121/" target="_blank">Intermediate Rails: Understanding Models, Views and Controllers</a></div>
  <div class="content"><p> I’m glad people liked the <a href="http://betterexplained.com/articles/starting-ruby-on-rails-what-i-wish-i-knew/">introduction to Rails</a>; now you scallawags get to avoid my headaches with the model-view-controller (MVC) pattern. This isn’t quite an <a href="http://slash7.com/articles/2005/2/22/mvc-the-most-vexing-conundrum">intro</a> to <span>MVC, </span>it’s a list of gotchas as you plod through <span>MVC </span>the first few times.</p>

<p>Here’s the big picture as I understand it:</p>

<p><img src="http://betterexplained.com/wp-content/uploads/rails/mvc-rails.png" alt="mvc.png" title="mvc.png" width="500" height="472" border="0"></p>


<ul>
<li>The <strong>browser</strong> makes a request, such as http://mysite.com/video/show/15</li>
</ul>




<ul>
<li>The <strong>web server</strong> (mongrel, <span>WEB</span>rick, etc.) receives the request. It uses <strong>routes</strong> to find out which controller to use: the default route pattern is “/controller/action/id” as defined in <code>config/routes.rb</code>. In our case, it’s the “video” controller, method “show”, id “15″. The web server then uses the <strong>dispatcher</strong> to create a new controller, call the action and pass the parameters.</li>
</ul>




<ul>
<li><strong>Controllers</strong> do the work of parsing user requests, data submissions, cookies, sessions and the “browser stuff”. They’re the pointy-haired manager that orders employees around. <strong>The best controller is Dilbert-esque:</strong> It gives orders without knowing (or caring) how it gets done. In our case, the show method in the video controller knows it needs to lookup a video. It asks the model to get video 15, and will eventually display it to the user.</li>
</ul>




<ul>
<li><strong>Models</strong> are Ruby classes. They talk to the database, store and validate data, perform the business logic and otherwise do the heavy lifting. <strong>They’re the chubby guy in the back room</strong> crunching the numbers. In this case, the model retrieves video 15 from the database.</li>
</ul>




<ul>
<li><strong>Views</strong> are what the user sees: <span>HTML, CSS, XML,</span> Javascript, <span>JSON.</span> They’re the sales rep putting up flyers and collecting surveys, at the manager’s direction. <strong>Views are merely puppets</strong> reading what the controller gives them. They don’t know what happens in the back room. In our example, the controller gives video 15 to the “show” view. The show view generates the <span>HTML</span>: divs, tables, text, descriptions, footers, etc.</li>
</ul>




<ul>
<li>The controller returns the response body (HTML, <span>XML, </span>etc.) &amp; metadata (caching headers, redirects) to the server. The server combines the raw data into a proper <span>HTTP </span>response and sends it to the user.</li>
</ul>



<p>It’s more fun to imagine a story with “fat model, skinny controller” instead of a sterile “3-tiered architecture”. Models do the grunt work, views are the happy face, and controllers are the masterminds behind it all.</p>

<p>Many <span>MVC </span>discussions ignore the role of the web server. However, it’s important to mention <strong>how</strong> the controller magically gets created and passed user information. The web server is the invisible gateway, shuttling data back and forth: users never interact with the controller directly.</p>

<h2>SuperModels</h2>

<p>Models are fat in Railsville: they do the heavy lifting so the controller stays lean, mean, and ignorant of the details. Here’s a few model tips:</p>

<p><strong>Using ActiveRecord</strong></p>



<pre>
<code>class User &lt; ActiveRecord::Base
end</code>
</pre>



<p>The code <code>&lt; ActiveRecord::Base</code> means your lowly User model inherits from class ActiveRecord::Base, and gets Rails magic to query and save to a database.</p>

<p>Ruby can also handle “undefined” methods with ease. ActiveRecord allows methods like “find_by_login”, which don’t actually exist. When you call “find_by_login”, Rails handles the “undefined method” call and searches for the “login” field. Assuming its in your database, it will do a query based on this field. There’s no configuration glue required.</p>

<p><hr></p>

<p><strong>Defining Class and Instance Methods</strong></p>


<pre>
<code> def self.foo 
    "Class method"    # User.foo
  end

  def bar
   "instance method"  # user.bar
  end</code>
</pre>



<p>Class and instance methods can cause confusion.</p>

<p><code>user</code> (lowercase u) is an object, and you call instance methods like <code>user.save</code>. </p>

<p><code>User</code> (capital U) is a class method - you don’t need an object to call it (like <code>User.find</code>). ActiveRecord adds both instance and class methods to your model. </p>

<p>As a tip, define class methods like <code>User.find_latest</code> rather than explicitly passing search conditions to <code>User.find</code> (thin controllers are better).</p>

<p><hr></p>

<p><strong>Using Attributes</strong></p>

<p>Regular Ruby objects can define attributes like this:</p>



<pre>
<code>
  # attribute in regular Ruby
  attr_accessor :name	     # like @name
  def name=(val)             # custom setter method
    @name = val.capitalize   # clean it up before saving
  end

  def name					 # custom getter
   "Dearest " + @name		 # make it nice
  end</code>
</pre>



<p>Here’s the deal:</p>


<ul>
<li><strong>attr_accessor :name</strong> creates get and set methods (name= and name) on your model. It’s like having a public instance variable <code>@name</code>.</li>
<li>Define method <code>name=(val)</code> to change how @name is saved (such as validating input). </li>
<li>Define method <code>name</code> to control how the variable is output (such as changing formatting). </li>
</ul>



<p>In Rails, attributes can be <a href="http://theocacao.com/document.page/187">confusing</a> because of the database magic. Here’s the deal:</p>


<ul>
<li>ActiveRecord grabs the database fields and throws them in an <code>attributes</code> array. It makes default getters and setters, but you need to call <code>user.save</code> to save them.</li>
<li>If you want to <b>override</b> the default getter and setter, use this:</li>
</ul>





<pre>
<code>
    # ActiveRecord: override how we access field
    def length=(minutes)
      self[:length] = minutes * 60
    end

    def length
      self[:length] / 60
    end
</code>
</pre>



<p>ActiveRecord defines a “<code>[]</code>” method to access the raw attributes (wraps the write_attribute and read_attribute). This is how you change the raw data. You can’t redefine length using</p>



<pre>
<code>def length			# this is bad
  length / 60
end
</code></pre>



<p>because it’s an infinite loop (and that’s no fun). So <code>self[]</code> it is. This was a particularly frustrating Rails headache of mine - when in doubt, use <code>self[:field]</code>.</p>

<p><hr></p>

<p><strong>Never forget you’re using a database</strong></p>

<p>Rails is clean. So clean, you forget you’re using a database. Don’t.</p>

<p><strong>Save your models.</strong> If you make a change, save it. It’s very easy to forget this critical step. You can also use update_attributes(params) and pass a hash of key -&gt; value pairs.</p>

<p><strong>Reload your models after changes</strong>. Suppose a user <code>has_many</code> videos. You create a new video, point it at the right user, and call user.videos to get a list. Will it work?</p>

<p>Probably not. If you already queried for videos, user.videos may have stale data. You need to call <code>user.reload</code> to get a fresh query. Be careful — the model in memory acts like a cache that can get stale.</p>

<p><hr><br>
<strong>Making New Models</strong></p>

<p>There’s two ways to create new objects:</p>



<pre>
<code>
joe = User.new( :name =&gt; &quot;Sad Joe&quot; )        # not saved
bob = User.create ( :name =&gt; &quot;Happy Bob&quot; )  # saved
</code>
</pre>




<ul>
<li><code>User.new</code> makes a new object, setting attributes with a hash. <code>new</code> <strong>does not save</strong> to the database: you must call <code>user.save</code> explicitly. Method <code>save</code> can fail if the model is not valid.</li>
<li><code>User.create</code> makes a new model and <strong>saves it to the database</strong>. Validation can fail; <code>user.errors</code> is a hash of the fields with errors and the detailed message.</li>
</ul>



<p>Notice how the hash is passed. With Ruby’s brace magic, <code>{}</code> is not explicitly needed so</p>



<pre><code>user = User.new( :name =&gt; &quot;kalid&quot;, :site =&gt; &quot;instacalc.com&quot; )</code></pre>


<p>becomes</p>



<pre><code>User.new( {:name =&gt; &quot;kalid&quot;, :site =&gt; &quot;instacalc.com&quot;} )</code></pre>



<p>The arrow (<code>=&gt;</code>) implies that a hash is being passed.</p>

<p><hr></p>

<p><strong>Using Associations</strong></p>

<p>Quick quiz, hotshot: suppose users have a “status”: active, inactive, pensive, etc. What’s the right association?</p>



<pre>
<code>
class User &lt; ActiveRecord::Base
  belongs_to :status  # this?
  has_one :status     # or this?
end
</code>
</pre>



<p>Hrm. Most likely, you want <code>belongs_to :status</code>. Yeah, it sounds weird. Don’t think about the phrase “has_one” and “belongs_to”, consider the meaning:</p>


<ul>
<li>belongs_to: <strong>links_to</strong> another table. Each user references (links to) a status.</li>
<li>has_one: <strong>linked_from</strong> another table. A status is linked_from a user. In fact, statuses don’t <strong>even know</strong> about users - there’s no mention of a “user” in the statuses table at all. Inside class Status we’d write <code>has_many :users</code> (has_one and has_many are the same thing - has_one only returns 1 object that <strong>links_to</strong> this one).</li>
</ul>



<p>A mnemonic:</p>


<ul>
<li>“belongs_to” rhymes with “links_to”</li>
<li>“has_one” rhymes with “linked_from”</li>
</ul>



<p>Well, they sort of rhyme. Work with me here, I’m trying to help.</p>

<p>These associations actually define methods used to lookup items of the other class. For example, “user belongs_to status” means that <code>user.status</code> queries the Status for the proper status_id. Also, “status has_many :users” means that <code>status.users</code> queries the user table for everyone with the current <code>status_id</code>. ActiveRecord handles the magic once we declare the relationship.</p>

<p><hr></p>

<p><strong>Using Custom Associations</strong></p>

<p>Suppose I need <strong>two</strong> statuses, primary and secondary? Use this:</p>



<pre><code>
belongs_to :primary_status, :model =&gt; &#39;Status&#39;, :foreign_key =&gt; &#39;primary_status_id&#39;
belongs_to :secondary_status, :model =&gt; &#39;Status&#39;, :foreign_key =&gt; &#39;secondary_status_id&#39;
</code></pre>



<p>You define a new field, and explicitly reference the model and foreign key to use for lookups. For example, user.primary_status returns a Status object with the id of “primary_status_id”. Very nice.</p>

<h2>Quick Controllers</h2>

<p>This section is short, because controllers shouldn’t do much besides boss the model and view around. They typically:</p>


<ul>
<li>Handle things like sessions, logins/authorization, filters, redirection, and errors.</li>
<li>Have default methods (added by ActionController). Visiting <code>http://localhost:3000/user/show</code> will attempt to call the “show” action if there is one, or automatically render show.rhtml if the action is not defined.</li>
<li><strong>Pass instance variables like @user get passed to the view</strong>. Local variables (those without @) don’t get passed.</li>
<li>Are hard to debug. Use <code>render :text =&gt; &quot;Error found&quot; and return</code> to do printf-style debugging in your page. This is another good reason to put code in models, which are easy to debug from the console.</li>
<li>Use sessions to store data between requests: session[:variable] = “data”.</li>
</ul>



<p>I’ll say it again because it’s burned me before: <strong>use @foo (not “foo”) to pass data to the view</strong>.</p>

<h2>Using Views</h2>

<p>Views are straightforward. The basics:</p>


<ul>
<li>Controller actions use views witht the same name (method <code>show </code> loads <code>show.rhtml</code> by default)</li>
<li>Controller instance variables (@foo) are available in all views and partials (wow!)</li>
</ul>



<p>Run code in a view using <span>ERB</span>:</p>


<ul>
<li><code>&lt;% ... %&gt;</code>: Run the code, but don’t print anything. Used for if/then/else/end and array.each loops. You can comment out sections of <span>HTML </span>using &lt;% if false %&gt; Hi there &lt;% end %&gt;. You get a free blank line, since you probably have a newline after the closing %&gt;.</li>
</ul>




<ul>
<li><code>&lt;%- ...  %&gt;</code>: Run the code, and don’t print the trailing newline. Use this when generating <span>XML </span>or <span>JSON </span>when breaking up .rhtml code blocks for your readability, but don’t want newlines in the output.</li>
</ul>




<ul>
<li><code>&lt;%= ... %&gt;</code>: Run the code and print the return value, for example: &lt;%= @foo %&gt; (You did remember the @ sign for controller variables passed to the view, right?). Don’t put <code>if</code> statements inside the <code>&lt;%=</code>, you’ll get an error.</li>
</ul>




<ul>
<li><code>&lt;%= h ... %&gt;</code>: Print the code and html escape the output: <code>&gt;</code> becomes <code>&amp;gt;</code>. h() is actually a Ruby function, but called without parens, as Rubyists are apt to do.</li>
</ul>



<p>It’s a bit confusing when you start out — run some experiments in a dummy view page.</p>

<h2>Take a breather</h2>

<p>The <span>MVC </span>pattern is a lot to digest in one sitting. As you become familiar with it, any Rails program becomes easy to dissect: it’s clear how the pieces fit together. <span>MVC </span>keeps your code nice and modular, great for debugging and maintenance.</p>

<p>In future articles I’ll discuss the inner details of <span>MVC </span>and how Rails forms work (another headache of mine). If you want a jumpstart on the nitty gritty, browse the <a href="http://dev.rubyonrails.org/browser/trunk/">Rails source</a> and try to follow the path of a request:</p>


<ul>
<li><a href="http://dev.rubyonrails.org/browser/trunk/railties/lib/webrick_server.rb"><span>WEB</span>rick server</a> modified to call the Rails routing library and dispatcher.</li>
<li><a href="http://dev.rubyonrails.org/browser/trunk/railties/lib/dispatcher.rb">Rails Dispatcher</a> actually creates the controller and passes it data.</li>
<li><a href="http://dev.rubyonrails.org/browser/trunk/actionpack/lib/action_controller/base.rb">ActionController Base</a> defines many functions, including those to call a controller action (using appropriate defaults), render text, and return a response.</li>
</ul>



<p>But all in good time my friends — I’ll explain it as I understand it. And if you had any forehead-slapping moments with <span>MVC, </span>drop me a note below.</p>

<p> </p>
<p><a href="http://feeds.feedburner.com/~a/Betterexplained?a=LE8yBs"><img src="http://feeds.feedburner.com/~a/Betterexplained?i=LE8yBs" border="0"></a></p><img src="http://feeds.feedburner.com/~r/Betterexplained/~4/143279121" height="1" width="1"></div>
</div>

<div class="feed">
  <div class="title">42. <a href="http://feeds.feedburner.com/~r/Betterexplained/~3/154352999/" target="_blank">Another Look at Prime Numbers</a></div>
  <div class="content"><p> Primes are numeric celebrities: they're used in movies, security codes, puzzles, and are even the subject of forlorn looks from university professors. </p>

<p>But mathematicians delight in finding the first 20 billion primes, rather than giving <strong>simple examples of why primes are useful and how they relate to what we know</strong>. Somebody else can discover the "largest prime" -- today let's share intuitive insights about why primes rock:</p>


<ul>
<li><strong>Primes are building blocks of all numbers.</strong> And just like in chemistry, knowing the chemical structure of a material helps understand and predict its properties. </li>
</ul>




<ul>
<li><strong>Primes have special properties</strong> like being difficult to determine (yes, even being difficult can be a positive trait). These properties have applications in cryptography, cycles, and seeing how other numbers multiply together.</li>
</ul>



<h2>So what are prime numbers again?</h2>

<p>A <a href="http://en.wikipedia.org/wiki/Fundamental_theorem_of_arithmetic">basic tenet</a> of math is that any number can be written as the multiplication of primes. For example:</p>


<ul>
<li>9 = 3 * 3 = 3<sup>2</sup></li>
<li>12 = 2 * 2 * 3 = 2<sup>2</sup> 3</li>
<li>100 = 4 * 25 = 2 * 2 * 5 * 5 = 2<sup>2</sup> 5<sup>2</sup></li>
</ul>



<p>And primes are numbers that can't be divided further, like 3, 5, 7, or 23. Even the number 2 is prime, if you think about it. And the number 1?</p>

<p>Well, 1 is special and isn't considered prime, since things get crazy because 1 = 1 * 1 * 1...  and so on. Even mathematicians take shortcuts sometimes, and leave 1 out of the discussion.</p>

<p>Rewriting a number into primes is called prime decomposition, math speak for "find the factors". Primes seem simple, right?</p>

<p>Well, not really. It turns out that</p>


<ul>
<li><strong>Primes are infinite</strong> and we'll never run out (<a href="http://primes.utm.edu/notes/proofs/infinite/">see proof</a>).</li>
<li><strong>Primes appear randomly distributed</strong></li>
<li><strong>Primes show up in strange places</strong>, like quantum mechanics</li>
<li><strong>Prime decomposition is hard.</strong> So far, trial-and-error is the best way to break a number into primes. And that's slow.</li>
</ul>



<p>God, nature, or the flying spaghetti monster -- whatever determined the primes, it made a whole lot of 'em and distributed them in a quirky way.</p>

<h2>Analogy: Prime Numbers and Chemical Formulas</h2>

<p>Prime numbers are like atoms. We can rewrite any number into its  "chemical formula" that shows its parts. In chemistry, we can say a water molecule is really H<sub>2</sub>0:</p>


<ul>
<li>Water = H<sub>2</sub>0 = two hydrogens and one oxygen </li>
</ul>



<p>And for a number, we can break it into primes</p>


<ul>
<li>12 = 2 * 2 * 3 = 2<sup>2</sup> 3 = two "2s" and one "3"</li>
</ul>



<p>Neat relationship, right? In chemistry the "exponent" happens to go underneath -- I'd really prefer exponents above, but the American Chemical Society hasn't replied to my letters.</p>

<p>Why is this interesting? Well, when chemists arranged their basic elements into the periodic table, new insights emerged:</p>


<ul>
<li>New elements were "predicted" by the gaps in the table</li>
<li>Elements in the same row or column shared certain properties </li>
<li>Trends (like increasing reactivity) emerged as you moved around the table</li>
</ul>



<p>Not bad for reorganizing existing data, eh? Similarly, we can imagine putting the primes (numerical "elements") into a table. But there's a problem.</p>

<p><strong>Nobody knows what the table looks like!</strong> Primes are infinite and appear at random. We've tried for centuries to find a pattern, but we can't. We have no idea where the gaps are or when the next prime is coming. (That's not quite true -- there's interesting hypotheses and conjectures, but the riddle is not solved).</p>

<p>But we won't cry about it, breaking our pencil and sobbing home. You and I are going to make use of the primes even though we don't know every detail.</p>

<h2>Organic Chemistry and Functional Groups</h2>

<p>I'm no chemistry expert, but I can see a relationship to the primes. Chemical elements have properties based on their location in the <a href="http://en.wikipedia.org/wiki/Periodic_table#Standard_periodic_table">periodic table of the elements</a>:</p>


<ul>
<li><strong>Atoms in group 18</strong> (Neon, Argon) are the noble gases. They don't react and won't blow up in your face. </li>
<li><strong>Atoms in group 14</strong> (Carbon, Silicon) bond well. They're great building blocks for other elements.</li>
<li><strong>Atoms in group 1</strong> (Sodium, Potassium, etc.) are very reactive. Drop 'em in water and see them explode.</li>
</ul>



<p>And in organic chemistry there's an idea of a functional group: several atoms can determine the class of the entire molecule. For example:</p>


<ul>
<li><a href="http://en.wikipedia.org/wiki/Alcohol">Alcohols</a> are a certain carbon-hydrogen chain with an OH group at the end.</li>
<li>Methanol, ethanol, and other alcohols share similar properties because of this OH functional group.</li>
</ul>



<p>Those are the basics, if I didn't mess it up. Now let's see what happens when we treat primes like chemicals.</p>

<h2>First Example: Guessing "Evenness"</h2>

<p>In general, an "organic" chemical contains carbon (not quite, but it's a good starting point). No matter what elements you mix together, if you never add any carbon then you can't create an organic compound.</p>

<p>"Evenness" works the same way. A number is even if it has a 2 in its prime decomposition -- i.e., 2 was used to "make" the number. There could be a single 2 or fifty; if you have a single 2, you are even, and that's that. If you don't have a 2, you're odd.</p>

<p>Now, remember those math questions that ask how odd and even numbers multiply?</p>


<ul>
<li>Even times odd is ... (even or odd?)</li>
<li>Even times even is ... (even or odd?)</li>
<li>Odd times odd is ... (even or odd?)</li>
</ul>



<p>How would you solve this? Guess? Try a few examples? ("Let's see, 3 times 2 is.. 6, but 3 times 3 is 9... so...").</p>

<p>Here's one way to think about it. Multiplication is combining the "prime formulas" for the numbers. Since even numbers contain a "2" somewhere, we can guess that:</p>


<ul>
<li>Even times odd is even. We started with a 2. It doesn't matter what else we put in.</li>
<li>Even times even is even. We started with a 2 and put in another for good measure.</li>
<li>Odd times odd is odd. We never put in a 2 the whole time, so we stay odd.</li>
</ul>



<p>Pretty cool, eh? And since 2 is prime, we know we can't "manufacture" a 2 by combining other numbers together.</p>

<p>Thank you prime chemistry, for giving us another way to think about this problem. Now you can even answer questions like this:</p>


<ul>
<li>What's odd * odd * odd * odd * even?</li>
</ul>



<p>It's even, since we mixed in a 2 at the end.</p>

<h2>Another Example: Ending with 0 </h2>

<p>I've read your mind: you want another chemical example, this time with functional groups.</p>

<p>Suppose a number has a "2*5" functional group -- it has one or more 2s and one or more 5s. For example:</p>


<ul>
<li>10 = 2 * 5</li>
<li>40 = 2 * 2 * 2 * 5</li>
<li>90 = 3 * 3 * 2 * 5</li>
</ul>



<p>Notice a pattern? If a number has a 2 * 5 "functional group", it ends in 0.</p>

<p>Why? Well, 2 * 5 = 10. So having 2 * 2 * 2 * 5 is really like having (2 * 2) * 10. Any whole number multiplied by 10 ends in 0. In general,</p>


<ul>
<li>(some other primes) * (2 * 5) = a number ending in 0</li>
</ul>



<p>So just by looking at the "prime formula" you can determine that the number ends with a 0. You never had to do the multiplication out.</p>

<h2>And Another Example: Sum of Digits</h2>

<p>What's that? You want another example with functional groups? If you insist.</p>

<p>Let's think about numbers with the "3*3" functional group. A number could have 400 threes, but as long as there's at least 2 we're interested. If a number has (3*3) it means</p>


<ul>
<li>It is divisible by 9</li>
<li>The sum of the digits is divisible by 9 (we can prove this later -- take my word for now).</li>
</ul>



<p>Here's an example:</p>


<ul>
<li>18 =  2 * 3 * 3. It has the (3*3) functional group. The sum of the digits is 1 + 8 = 9, which is divisible by 9.</li>
<li>Take a strange number like 31 * 3 * 3 = 279. It has a (3*3) functional group, and the sum of digits is 2 + 7 + 9 = 18. 18 is divisible by 9, so the property holds.</li>
</ul>



<p>Again, this is pretty cool. We know something about the sum of digits just by finding a certain functional group in the prime decomposition of the number.</p>

<h2>Primes in the Real World</h2>

<p>Primes have properties that come in useful.</p>

<p><strong>1. Large numbers are hard to factor.</strong> We essentially resort to trial-and-error when doing prime decomposition: one method is to keep trying to divide it by other numbers, up to its square root. The fact that primes and prime decompositions are "secret" can be a good thing for cryptography -- we'll get into this later.</p>

<p><strong>2. Primes don't play well with other numbers.</strong> Prime numbers don't "overlap" with the regular numbers: they intersect at the last possible moment. For example, 4 and 6 "overlap" at 12, which is pretty early. Their first "required" overlap is at 4 * 6 = 24.</p>

<p>Primes, however, intersect at the last possible moment. 5 and 7, for example, only coincide at 35 (5*7). There's no intermediate value where they both show up.</p>

<p>You'd think a lack of rhythm would be a bad thing, but in nature it can be an advantage.</p>

<p>The cicada insect sprouts from the ground every 13 or 17 years. This means it has a smaller chance of "overlapping" with a predator's cycle, which could be at a more common 2 or 4-year cycle.</p>

<p><strong>3. Primes are prime everywhere.</strong></p>

<p>The movie "Contact" used primes as a universally understood sequence. It's a non-trivial sequence (2, 3, 5, 7, 11, 13) that would be hard to generate by accident (1, 0, 1, 0 could be made by a swinging pendulum, for example). </p>

<p>And prime numbers are prime in <a href="http://betterexplained.com/articles/numbers-and-bases/">any number system</a>. "1/3" is only a repeating fraction in base 10 (.33333), and you could even argue that pi (3.14159...) is not irrational in base "pi". But everyone can agree that certain numbers are prime and can't be divided. You can even transmit primes in a unary number system that lacks a decimal point:</p>

<p>II<br>
<span>III</span><br>
<span>IIIII</span><br>
<span>IIIIIII</span></p>

<p>etc. So primes are an infinite, non-repeating, universally-understood sequence, and a good choice for transmitting a message.</p>

<h2>Conclusion</h2>

<p>Don't hate the primes because they're different -- <strong>see how their properties can be useful</strong>. "Not fitting in" is a great if it means you don't overlap with a predator, right? Being hard to factor is great if you're making a secret message, right? For a long time primes were considered a purely theoretical curiosity, but lo and behold, we've found situations where they apply.</p>

<p>And that's a large part of math, in my opinion: seeing how strange properties can be useful or relate to the real-world. Math gives us rules, often for games we don't yet play. Our job is to find situations where we <strong>want</strong> to follow those rules.</p>

<p>There's much more I'd like to say in upcoming posts. If you want to dive into primes, check out <a href="http://www.google.com/url?sa=t&amp;%23038;ct=res&amp;%23038;cd=4&amp;%23038;url=http%3A%2F%2Fwww.amazon.com%2FMusic-Primes-Searching-Greatest-Mathematics%2Fdp%2F0066210704&amp;%23038;ei=ujbkRru0M4_CetfE7JEK&amp;%23038;usg=AFQjCNGonjI32zVsuC64KovEoOdjnQ15Wg&amp;%23038;sig2=q7EhnLYemIaDTmVVx_nFMg">Music of the primes</a> which is a decent introduction to the issue of the primes, and motivated me to think about this topic. </p><img src="http://feeds.feedburner.com/~r/Betterexplained/~4/154352999" height="1" width="1"></div>
</div>

<div class="feed">
  <div class="title">43. <a href="http://feeds.feedburner.com/~r/Betterexplained/~3/162179936/" target="_blank">A Visual Guide to Version Control</a></div>
  <div class="content"><p> <img src="http://betterexplained.com/wp-content/uploads/version_control/version_control_intro_small.png" align="center" border="0"></p>

<p>Version Control (aka Revision Control aka Source Control) lets you track your files over time. Why do you care? So when you mess up you can easily get back to a previous working version.</p>

<p><strong>You’ve probably cooked up your own</strong> version control system without realizing it had such a geeky name. Got any files like this? (Not these exact ones I hope).</p>


<ul>
<li>KalidAzadResumeOct2006.doc</li>
<li>KalidAzadResumeMar2007.doc</li>
<li>instacalc-logo3.png</li>
<li>instacalc-logo4.png</li>
<li>logo-old.png</li>
</ul>



<p><strong>It’s why we use “Save As”.</strong> You want the new file without obliterating the old one. It’s a common problem, and solutions are usually like this:</p>


<ul>
<li>Make a <strong>single backup copy</strong> (Document.old.txt). </li>
<li>If we’re clever, we add a <strong>version number or date</strong>: Document_V1.txt, DocumentMarch2007.txt</li>
<li>We may even use a <strong>shared folder</strong> so other people can see and edit files without sending them over email. Hopefully they relabel the file after they save it.</li>
</ul>



<h2>So Why Do We Need A Version Control System (VCS)?</h2>

<p>Our shared folder/naming system is fine for class projects or one-time papers. But software projects? Not a chance.</p>

<p>Do you think the Windows source code sits in a shared folder like “Windows2007-Latest-UPDATED!!”, for anyone to edit? That every programmer just works in a different subfolder? No way.</p>

<p>Large, fast-changing projects with many authors need a Version Control System (geekspeak for “file database”) to track changes and avoid general chaos. A good <span>VCS </span>does the following:</p>


<ul>
<li><strong>Backup and Restore.</strong> Files are saved as they are edited, and you can jump to any moment in time. Need that file as it was on Feb 23, 2007? No problem.</li>
<li><strong>Synchronization.</strong> Lets people share files and stay up-to-date with the latest version.</li>
<li><strong>Short-term undo.</strong> Monkeying with a file and messed it up? (That’s just like you, isn’t it?). Throw away your changes and go back to the “last known good” version in the database.</li>
<li><strong>Long-term undo.</strong> Sometimes we mess up bad. Suppose you made a change a year ago, and it had a bug. Jump back to the old version, and see what change was made that day.</li>
<li><strong>Track Changes</strong>. As files are updated, you can leave messages explaining why the change happened (stored in the <span>VCS, </span>not the file). This makes it easy to see how a file is evolving over time, and why.</li>
<li><strong>Track Ownership.</strong> A <span>VCS </span>tags every change with the name of the person who made it. Helpful for <a href="http://www.unwords.com/unword/blamestorming.html"><del>blamestorming</del></a> giving credit.</li>
<li><strong>Sandboxing</strong>, or insurance against yourself. Making a big change? You can make temporary changes in an isolated area, test and work out the kinks before “checking in” your changes.</li>
<li><strong>Branching and merging</strong>. A larger sandbox. You can <strong>branch</strong> a copy of your code into a separate area and modify it in isolation (tracking changes separately). Later, you can <strong>merge</strong> your work back into the common area.</li>
</ul>



<p>Shared folders are quick and simple, but can’t beat these features.</p>

<h2>Learn the Lingo</h2>

<p>Most version control systems involve the following concepts, though the labels may be different.</p>

<p>Basic Setup</p>


<ul>
<li><strong>Repository (repo)</strong>: The database storing the files. </li>
<li><strong>Server</strong>: The computer storing the repo.</li>
<li><strong>Client</strong>: The computer connecting to the repo.</li>
<li><strong>Working Set/Working Copy</strong>: Your local directory of files, where you make changes.</li>
<li><strong>Trunk/Main</strong>: The “primary” location for code in the repo. Think of code as a family tree — the “trunk” is the main line.</li>
</ul>



<p>Basic Actions</p>


<ul>
<li><strong>Add</strong>: Put a file into the repo for the first time, i.e. begin tracking it with Version Control.</li>
<li><strong>Revision</strong>: What version a file is on (v1, v2, v3, etc.).</li>
<li><strong>Head</strong>: The latest revision in the repo.</li>
<li><strong>Check out</strong>: Download a file from the repo.</li>
<li><strong>Check in</strong>: Upload a file to the repository (if it has changed). The file gets a new revision number, and people can “check out” the latest one.</li>
<li><strong>Checkin Message</strong>: A short message describing what was changed.</li>
<li><strong>Changelog/History</strong>: A list of changes made to a file since it was created.</li>
<li><strong>Update/Sync</strong>: Synchronize your files with the latest from the repository. This lets you grab the latest revisions of all files.</li>
<li><strong>Revert</strong>: Throw away your local changes and reload the latest version from the repository.</li>
</ul>



<p>Advanced Actions</p>


<ul>
<li><strong>Branch</strong>: Create a separate copy of a file/folder for private use (bug fixing, testing, etc). Branch is both a verb (”branch the code”) and a noun (”Which branch is it in?”).</li>
<li><strong>Diff/Change/Delta</strong>: Finding the differences between two files. Useful for seeing what changed between revisions.</li>
<li><strong>Merge (or patch)</strong>: Apply the changes from one file to another, to bring it up-to-date. For example, you can merge features from one branch into another. (At Microsoft this was called <a href="http://blogs.msdn.com/larryosterman/archive/2005/02/01/364840.aspx">Reverse Integrate and Forward Integrate</a>)</li>
<li><strong>Conflict</strong>: When pending changes to a file contradict each other (both changes cannot be applied).</li>
<li><strong>Resolve</strong>: Fixing the changes that contradict each other and checking in the correct version.</li>
<li><strong>Locking</strong>: “Taking control” of a file so nobody else can edit it until you unlock it. Some version control systems use this to avoid conflicts.</li>
<li><strong>Breaking the lock</strong>: Forcibly unlocking a file so you can edit it. It may be needed if someone locks a file and goes on vacation (or “calls in sick” the day Halo 3 comes out).</li>
<li><strong>Check out for edit</strong>: Checking out an “editable” version of a file. Some <span>VCS</span>es have editable files by default, others require an explicit command.</li>
</ul>



<p>And a typical scenario goes like this:</p>

<p>Alice <strong>adds</strong> a file (<code>list.txt</code>) to the <strong>repository</strong>. She <strong>checks it out</strong>, makes a change (puts “milk” on the list), and checks it back in with a checkin message (”Added required item.”). The next morning, Bob <strong>updates</strong> his local working set and sees the latest revision of <code>list.txt</code>, which contains “milk”. He can browse the <strong>changelog</strong> or <strong>diff</strong> to see that Alice put “milk” the day before.</p>

<h2>Visual Examples</h2>

<p>This guide is purposefully high-level: most tutorials throw a bunch of text commands at you. I prefer to cover the high-level concepts without getting stuck in the syntax (the manual is always there don’t worry). Sometimes it’s nice to <strong>see what’s possible</strong>.</p>

<h2>Checkins</h2>

<p>The simplest scenario is checking in a file (<code>list.txt</code>) and modifying it over time.</p>

<p><img src="http://betterexplained.com/wp-content/uploads/version_control/basic_checkin.png" alt=""></p>

<p>Each time we check in a new version, we get a new revision (r1, r2, r3, etc.).</p>



<pre>
<code>svn add list.txt
(modify the file)
svn ci list.txt -m "Changed the list"</code>
</pre>



<p>The <code>-m</code> flag is the message to use for this checkin.</p>

<h2>Checkouts and Editing</h2>

<p>In reality, you might not keep checking in a file. You may have to <strong>check out, edit and check in</strong>. The cycle looks like this:</p>

<p><img src="http://betterexplained.com/wp-content/uploads/version_control/checkout_edit.png" alt=""></p>

<p>If you don’t like your changes and want to start over, you can <strong>revert</strong> to the previous version and start again (or stop). When checking out, you get the latest revision by default. If you want, you can specify a particular revision. In Subversion, run:</p>



<pre>
<code>
svn co list.txt (get latest version)
...edit file...
svn revert list.txt (throw away changes)

svn co -r2 list.txt (check out particular version)
</code>
</pre>



<h2>Diffs</h2>

<p>The trunk has a history of <strong>changes</strong> as a file evolves. Diffs are the changes you made while editing: imagine you can “peel” them off and apply them to a file:</p>

<p><img src="http://betterexplained.com/wp-content/uploads/version_control/basic_diffs.png" alt=""></p>

<p>For example, to go from r1 to r2, we add eggs (+Eggs). Imagine peeling off that red sticker and placing it on r1, to get r2.</p>

<p>And to get from r2 to r3, we add Juice (+Juice). To get from r3 to r4, we remove Juice and add Soup (-Juice, +Soup).</p>

<p>Most version control systems <strong>store diffs rather than full copies of the file</strong>. This saves disk space: 4 revisions of a file doesn’t mean we have 4 copies; we have 1 copy and 4 small diffs. Pretty nifty, eh? In <span>SVN </span>we can write this</p>



<pre>
<code>svn diff -r3:4 list.txt</code>
</pre>



<p>Diffs help us notice changes (”How did you fix that bug again?”) and even apply them from one branch to another.</p>

<p><strong>Bonus question:</strong> what’s the diff from r1 to r4?</p>



<pre>
<code>+Eggs
+Soup</code>
</pre>



<p>Notice how “Juice” wasn’t even involved — the direct jump from r1 to r4 doesn’t need that change, since Juice was overridden by Soup.</p>

<h2>Branching</h2>

<p>Branches let us copy code into a separate folder so we can monkey with it separately:</p>

<p><img src="http://betterexplained.com/wp-content/uploads/version_control/first_branch.png" alt=""></p>

<p>For example, we can create a branch for new, experimental ideas for our list: crazy things like Rice or Eggo waffles. Depending on the version control system, creating a branch (copy) may change the revision number.</p>

<p>Now that we have a branch, we can change our code and work out the kinks. (<i>“Hrm… waffles? I don’t know what the boss will think. Rice is a safe bet.”</i>). Since we’re in a separate branch, we can make changes and test in isolation, knowing our changes won’t hurt anyone. And our branch history is under version control.</p>

<p>In Subversion, you create a branch simply by copying a directory to another.</p>



<pre>
<code>svn copy http://path/to/trunk http://path/to/branch</code>
</pre>



<p>So branching isn’t too tough of a concept: <strong>Pretend you copied your code into a different directory.</strong> You’ve probably branched your code in school projects, making sure you have a “fail safe” version you can return to if things blow up.</p>

<h2>Merging</h2>

<p>Branching sounds simple, right? Well, it’s not — figuring out how to merge changes from one branch to another can be tricky.</p>

<p>Let’s say we want to get the “Rice” feature from our experimental branch into the mainline. How would we do this? Diff r6 and r7 and apply that to the main line?</p>

<p><strong>Wrongo.</strong> We only want to apply the changes <strong>that happened in the branch!</strong>. That means we diff r5 and r6, and apply that to the main trunk:</p>

<p><img src="http://betterexplained.com/wp-content/uploads/version_control/merging.png" alt=""></p>

<p>If we diffed r6 and r7, we would lose the “Bread” feature that was in main. This is a subtle point — imagine “peeling off” the changes from the experimental branch (+Rice) and adding that to main. Main may have had other changes, which is ok — we just want to insert the Rice feature.</p>

<p>In Subversion, merging is very close to diffing. Inside the main trunk, run the command: </p>



<pre>
<code>svn merge -r5:6 http://path/to/branch</code>
</pre>



<p>This command diffs r5-r6 in the experimental branch and applies it to the current location. Unfortunately, Subversion doesn’t have an easy way to keep track of what merges have been applied, so if you’re not careful you may apply the same changes twice. It’s a planned feature, but the current advice is to keep a changelog message reminding you that you’ve already merged r5-r6 into main.</p>

<h2>Conflicts</h2>

<p>Many times, the <span>VCS </span>can automatically merge changes to different parts of a file. <strong>Conflicts</strong> can arise when changes appear that don’t gel: one person wants to remove eggs and replace it with cheese (-eggs, +cheese), and you want to replace eggs with a hot dog (-eggs, +hot dog).</p>

<p>When changes contradict like this, the <span>VCS </span>may report a <strong>conflict</strong> and not let you check in — it’s up to you to check in a newer version that <strong>resolves</strong> this dilemma. A few approaches:</p>


<ul>
<li><strong>Re-apply your changes</strong>. Revert to the the latest version and re-apply your changes: (Add hot dog to the list that has cheese).</li>
<li><strong>Override their changes with yours</strong>. Check out the latest version, copy over your version, and check your version in, effectively replacing cheese with hot dog.</li>
</ul>



<p>Conflicts are infrequent but can be a pain. Usually I update to the latest and re-apply my changes.</p>

<h2>Tagging</h2>

<p>Who would have thought a version control system would be Web 2.0 compliant? Many systems let you tag (label) any revision for easy reference. This way you can refer to “Release 1.0″ instead of a particular build number:</p>

<p><img src="http://betterexplained.com/wp-content/uploads/version_control/tagging.png" alt=""></p>

<p>In Subversion, tags are just branches that you agree not to edit; they are around for posterity, so you can see exactly what your version 1.0 release contained. Hence they end in a stub — there’s nowhere to go.</p>



<pre>
<code>(in trunk)
svn copy http://path/to/revision http://path/to/tag
</code>
</pre>



<h2>Real-life example: Managing Windows Source Code</h2>

<p>We guessed that Windows was managed out of a shared folder, but it’s not the case. So <a href="http://blogs.msdn.com/larryosterman/archive/2005/02/01/364840.aspx">how’s it done</a>?</p>


<ul>
<li>There’s a <strong>main line</strong> with stable builds of Windows.</li>
<li>Each group (Networking, User Interface, Media Player, etc.) <strong>has its own branch</strong> to develop new features. These are under development and less stable than main.</li>
</ul>



<p>You develop new features in your branch and “Reverse Integrate (RI)” to get them into Main. Later, you “Forward Integrate” and to get the latest changes from Main into your branch:</p>

<p><img src="http://betterexplained.com/wp-content/uploads/version_control/windows.png" alt=""></p>

<p>Let’s say we’re at Media Player 10 and IE 6. The Media Player team makes version 11 in their own branch. When it’s ready and tested, there’s a patch from 10 - 11 which is applied to Main (just like the “Rice” example, but a tad more complicated). This a <strong>reverse integration</strong>, from the branch to the trunk. The IE team can do the same thing.</p>

<p>Later, the Media Player team can pick up the latest code from other teams, like <span>IE.</span> In this case, Media Player <strong>forward integrates</strong> and gets the latest patches from main into their branch. This is like pulling in the “Bread” feature into the experimental branch, but again, more complicated.</p>

<p>So it’s RI and <span>FI.</span> Aye aye. This arrangement lets changes percolate throughout the branches, while keeping new code out of the main line. Cool, eh?</p>

<p>In reality, there’s many layers of branches and sub-branches, along with quality metrics that determine when you get to <span>RI.</span> But you get the idea: branches help manage complexity. Now you know the basics of how one of the largest software projects is organized.</p>

<h2>Key Takeaways</h2>

<p>My goal was to share high-level thoughts about version control systems. Here are the basics:</p>


<ul>
<li><strong>Use version control.</strong> Seriously, it’s a good thing, even if you’re not writing an <span>OS.</span> It’s worth it for backups alone.</li>
<li><strong>Take it slow.</strong> I’m only now looking into branching and merging for my projects. Just get a handle on using version control and go from there. If you’re a small project, branching/merging may not be an issue. Large projects often have experienced maintainers who keep track of the branches and patches.</li>
<li><strong>Keep Learning.</strong> There’s plenty of guides for <a href="http://svnbook.red-bean.com/"><span>SVN</span></a>, <a href="http://wwwasd.web.cern.ch/wwwasd/cvs/tutorial/cvs_tutorial_toc.html"><span>CVS</span></a>, <a href="http://agave.garden.org/~aaronh/rcs/tutorial.html"><span>RCS</span></a>, <a href="http://www.kernel.org/pub/software/scm/git/docs/tutorial.html">Git</a>, <a href="http://public.perforce.com/public/tutorial.html">Perforce</a> or whatever system you’re using. The important thing is to <strong>know the concepts</strong> and realize every system has its own lingo and philosophy. <a href="http://www.ericsink.com/scm/source_control.html">Eric Sink</a> has a detailed guide as well.</li>
</ul>



<p>These are the basics — as time goes on I’ll share specific lessons I’ve learned from <a href="http://instacalc.com">my projects</a>. Now that you’ve figured out a regular <span>VCS, </span><a href="http://betterexplained.com/articles/intro-to-distributed-version-control-illustrated/">try an illustrated guide to distributed version control</a>. </p><img src="http://feeds.feedburner.com/~r/Betterexplained/~4/162179936" height="1" width="1"></div>
</div>

<div class="feed">
  <div class="title">44. <a href="http://feeds.feedburner.com/~r/Betterexplained/~3/166610380/" target="_blank">What do you want Better Explained?</a></div>
  <div class="content"><p> First, a big thank you. I’m so excited that this site has been able to reach more people than I thought possible — I love hearing about and exchanging insights <img src="http://betterexplained.com/wp-includes/images/smilies/icon_smile.gif" alt=":)"> .</p>

<p><img src="http://betterexplained.com/wp-content/uploads/stats/feedburner_stats_oct07.png" alt=""></p>

<p>Still, there’s <strong>only so much juice</strong> you can squeeze from this orange at a time. The <a href="http://betterexplained.com/articles/a-visual-guide-to-version-control/">previous article</a> took several days to research, draft, diagram and edit (I hope to get faster over time).</p>

<p>And that’s where you fit in: I’ve had some great email conversations about expanding the site, and I’d like to get everyone’s thoughts. Are blogs, guest posts, wikis, or forums the best way to share?</p>

<h2>The Goal: Cram Ideas Into Your Head</h2>

<p>Taking a step back, <a href="http://betterexplained.com/about/">my philosophy</a> is simple: Let’s share the hard-won insights that usually stay locked in our heads.</p>

<p><strong>Learning is like crossing a minefield</strong>. There’s pitfalls everywhere, but after a struggle you make it through (or get “permanently discouraged”). But once you’ve navigated the path, shouldn’t you share it with those behind you? While it’s still fresh in your mind?</p>

<p><strong>It frustrates me</strong> that we study the same subjects (algebra, calculus, programming), but struggle alone. We don’t share “a-ha” insights that really make learning stick; most textbooks offer pedantic facts and rote memorization. We have enough of those — what about the insights that help us “really get it”:</p>


<ul>
<li>e is a <a href="http://betterexplained.com/articles/an-intuitive-guide-to-exponential-functions-e/">fundamental rate of growth</a>, just like pi a fundamental ratio in every circle</li>
<li><a href="http://betterexplained.com/articles/a-visual-guide-to-version-control/">Version control</a> is like keeping a timeline of your files</li>
<li><a href="http://betterexplained.com/articles/how-to-optimize-your-site-with-http-caching/"><span>HTTP</span> Caching</a> lets you avoid downloading a file you already have</li>
</ul>



<h2>Models For Online Communities</h2>

<p>Having this blog has been a huge learning experience (more to come), and there’s much to learn about starting a community. Though it’s still a small site, I’m looking to my favorite big sites for patterns of interaction:</p>


<ul>
<li><strong>Regular blog (<a href="http://codinghorror.com">Coding Horror</a>, <a href="http://techcrunch.com">TechCrunch</a>)</strong>: Single or many authors start the discussion with a detailed article. Readers reply with their own thoughts. There’s consistent style and quality, but topics are limited to what the authors discuss.</li>
<li><strong>Edited submissions (<a href="http://slashdot.org">Slashdot</a>):</strong> Site owners collect and filter topic submissions; readers contribute their thoughts. The majority of the value is in the comments, not the stories or writeups themselves.</li>
<li><strong>Community submissions (<a href="http://digg.com">Digg</a>, <a href="http://reddit.com">Reddit</a>):</strong> The community decides the topics and handles the comments as well. The site owners are seemingly invisible. This works well for late-breaking news, but can lead to “tyranny of the crowd”.</li>
<li><strong>Forums (<a href="http://stevepavlina.com/forums">Steve Pavlina</a>, <a href="http://www.webmasterworld.com/">Webmaster World</a>)</strong>: Users kick off their own topic ideas. Forums have fast, free-flowing discussion but can have a low signal-to-noise ratio.</li>
<li><strong>Wikis (<a href="http://wikipedia.com">Wikipedia</a>)</strong>: Edited repository of information containing collected thoughts. Unfortunately there’s a high barrier to entry — how often do you contribute something substantial to a wiki? It’s nice to have your own voice shine through in a forum post or comment.</li>
</ul>



<p>Those are the major collaboration models I’ve found.</p>

<h2>Moving Forward: What would you like to see?</h2>

<p>I’m a fan of the “ready-fire-aim” approach, trying something new and tweaking as you go. So here goes:</p>


<ul>
<li><strong>Share a comment</strong> on what type of collaboration you enjoy most. I’m looking more into wiki and forum software; perhaps a hybrid will do.</li>
<li><strong>Suggest topics</strong> you’d like to see explained better. More math? More programming topics? More business/writing/communication?</li>
<li><a href="http://betterexplained.com/contact/"><strong>Contact me</strong></a> (kalid@instacalc.com) if you’d like to do a guest post. Just think of a topic you like — the content is yours. I can help with formatting/proofreading, even diagrams if you like. I’m thinking of making a guide to writing “how-to” guides as well.</li>
</ul>



<p>My dream is to contribute to a community that helps us all avoid learning’s stumbling blocks. There’s so many ways to present a topic and yet so many “a ha!” moments are just locked away in our brains.</p>

<p>And what’s the incentive to share your understanding? Well, if you’re like me, you’ll forget your insights and need them explained again. And who better to teach a topic than you, when you remembered it?</p>

<p>Thanks again for all the support and comments — it makes writing a pleasure (editing is still a pain, though! <img src="http://betterexplained.com/wp-includes/images/smilies/icon_smile.gif" alt=":)"> ). I’m excited for what the next months and years will bring. </p><img src="http://feeds.feedburner.com/~r/Betterexplained/~4/166610380" height="1" width="1"></div>
</div>

<div class="feed">
  <div class="title">45. <a href="http://feeds.feedburner.com/~r/Betterexplained/~3/170014927/" target="_blank">Intro to Distributed Version Control (Illustrated)</a></div>
  <div class="content"><p> <img src="http://betterexplained.com/wp-content/uploads/version_control/distributed/distributed_logo.png" alt=""></p>

<p>Traditional version control helps you backup, track and synchronize files. Distributed version control makes it easy to share changes. Done right, you can get the best of both worlds: simple merging and centralized releases.</p>

<h2>Distributed? What’s wrong with regular version control?</h2>

<p>Nothing — read <a href="http://betterexplained.com/articles/a-visual-guide-to-version-control/">a visual guide to version control</a> if you want a quick refresher. Sure, <em>some people</em> will deride you for using an “ancient” system. But you’re still OK in my book: using <em>any</em> <span>VCS </span>is a positive step forward for a project.</p>

<p>Centralized <span>VCS </span>emerged from the 1970s, when programmers had thin clients and admired “big iron” mainframes (how can you <strong>not</strong> like a machine with a then-gluttonous <a href="http://en.wikipedia.org/wiki/System/360">8 bits to a byte</a>?).</p>

<p><strong>Centralized is simple</strong>, and what you’d first invent: a single place everyone can check in and check out. It’s like a library where you get to scribble in the books.</p>

<p>This model works for <strong>backup, undo and synchronization</strong> but isn’t great for <strong>merging and branching</strong> changes people make. As projects grow, you want to split features into chunks, developing and testing in isolation and slowly merging changes into the main line. In reality, branching is cumbersome, so new features may come as a giant checkin, making changes difficult to manage and untangle if they go awry.</p>

<p>Sure, merging is always “possible” in a centralized system, but it’s not easy: you often need to track the merge yourself to avoid making the same change twice. Distributed systems make branching and merging painless because they rely on it.</p>

<h2>A Few Diagrams, Please</h2>

<p>Other tutorials have plenty of nitty-gritty text commands; here’s a <strong>visual</strong> look. To refresh, developers use a central repo in a typical <span>VCS</span>:</p>

<p><img src="http://betterexplained.com/wp-content/uploads/version_control/distributed/centralized_example.png" alt=""></p>

<p>Everyone syncs and checks into the main trunk. Sue’s changes must go into main before they can be seen. Yes, theoretically Sue <em>could</em> make a branch for others to test changes, but this is a pain in a regular <span>VCS.</span></p>

<h2>Distributed Version Control Systems (DVCS)</h2>

<p>In a <strong>distributed</strong> model, every developer has their own repo. Sue’s changes live in <strong>her local repo</strong>, which she can share with Joe or Eve:</p>

<p><img src="http://betterexplained.com/wp-content/uploads/version_control/distributed/distributed_example.png" alt=""></p>

<p>But will it be a circus with no ringleader? Nope. If desired, everyone can push changes into a common repo, suspiciously like the centralized model above. This franken-repo contains the changes of Sue, Joe and Eve.</p>

<p><strong>I wish distributed version control had a different name</strong>, such as “independent”, “federated” or “peer-to-peer.” The term “distributed” evokes thoughts of distributed computing, where work is split among a grid of machines (like searching for signals with <a href="http://setiathome.berkeley.edu/"><span>SETI</span>@home</a> or doing <a href="http://folding.stanford.edu/">protein folding</a>).</p>

<p>A <span>DVCS </span>is not like Seti@home: each node is completely independent and sharing is optional (in Seti you must phone back your results).</p>

<h2>Key Concepts In 5 Minutes</h2>

<p>Here’s the basics; there’s a <a href="http://en.wikibooks.org/wiki/Understanding_darcs/Patch_theory">book</a> on patch theory if you’re interested.</p>

<p><strong>Core Concepts</strong></p>


<ul>
<li>Centralized version control focuses on <strong>synchronizing, tracking, and backing up files.</strong></li>
<li>Distributed version control focuses on <strong>sharing changes</strong>; every change has a <a href="http://betterexplained.com/articles/the-quick-guide-to-guids/">guid or unique id</a>.</li>
<li><strong>Recording/Downloading</strong> and <strong>applying</strong> a change are separate steps (in a centralized system, they happen together).</li>
<li><strong>Distributed systems have no forced structure</strong>. You can create “centrally administered” locations or keep everyone as peers.</li>
</ul>



<p><strong>New Terminology</strong></p>


<ul>
<li><strong>push</strong>: send a change to another repository (may require permission)</li>
<li><strong>pull</strong>: grab a change from a repository</li>
</ul>



<p><strong>Key Advantages</strong></p>


<ul>
<li><strong>Everyone has a local sandbox.</strong> You can make changes and roll back, all on your local machine. No more giant checkins; your incremental history is in your repo. </li>
<li><strong>It works offline.</strong> You only need to be online to share changes. Otherwise, you can happily stay on your local machine, checking in and undoing, no matter if the “server” is down or you’re on an airplane.</li>
<li><strong>It’s fast.</strong> Diffs, commits and reverts are all done locally. There’s no shaky network or server to ask for old revisions from a year ago.</li>
<li><strong>It handles changes well.</strong> Distributed version control systems were <em>built</em> around sharing changes. Every change has a guid which makes it easy to track.</li>
<li><strong>Branching and merging is easy.</strong> Because every developer “has their own branch”, every shared change is like reverse integration. But the guids make it easy to automatically combine changes and avoid duplicates.</li>
<li><strong>Less management.</strong> Distributed <span>VCS</span>es are easy to get running; there’s no “always-running” server software to install. Also, <span>DVCS</span>es may not require you to “add” new users; you just pick what <span>URL</span>s to pull from. This can avoid political headaches in large projects.</li>
</ul>



<p><strong>Key Disadvantages</strong></p>


<ul>
<li><strong>You still need a backup.</strong> Some claim your “backup” is the other machines that have your changes. I don’t buy it — what if they didn’t accept them all? What if they’re offline and you have new changes? With a <span>DVCS, </span>you still want a machine to push changes to “just in case”. (In Subversion, you usually dedicate a machine to store the main repo; do the same for a <span>DVCS</span>).</li>
<li><strong>There’s not really a “latest version”</strong>. If there’s no central location, you don’t immediately know whether to see Sue, Joe or Eve for the latest version. Again, a central location helps clarify what the latest “stable” release is.</li>
<li><strong>There aren’t really revision numbers.</strong> Every repo has its own revision numbers depending on the changes. Instead, people refer to change numbers: <em>Pardon me, do you have change fa33e7b?</em> (Remember, the id is an ugly guid). Thankfully, you can tag releases with meaningful names.</li>
</ul>



<h2>Mercurial Quickstart</h2>

<p>Mercurial is a fast, simple <span>DVCS.</span> The nickname is hg, like the element Mercury.</p>



<pre>
<code>
cd project
hg init                                (create repo here)
hg add list.txt                        (start tracking file)
hg commit -m "Added file"              (check file into local repo)
hg log                                 (see history; notice guid)

changeset:   0:55bbcb7a4c24
user:        Kalid@kazad-laptop
date:        Sun Oct 14 21:36:18 2007 -0400
summary:     Added file

[edit file]
hg revert list.txt                 (revert to previous version)

hg tag v1.0                        (tag this version)
[edit file]
hg update -C v1.0                  ("update" to the older tagged version; -C forces overwrite of local copy)
</code>
</pre>



<p>Once Mercurial has initialized a directory, it looks like this:</p>

<p><img src="http://betterexplained.com/wp-content/uploads/version_control/distributed/distributed_repo_layout.png" alt=""></p>

<p>You have:</p>


<ul>
<li><strong>A working copy</strong>. The files you are currently editing.</li>
<li><strong>A repository</strong>. A directory (.hg in Mercurial) containing all patches and metadata (comments, guids, dates, etc.). There’s no central server so the data stays with you.</li>
</ul>



<p>In our distributed example, Sue, Joe and Eve have their own repos, with independent revision histories.</p>

<h2>Understanding Updates and Merging</h2>

<p>There’s a few items that confused me when learning about <span>DVCS.</span> First, updates happen in several steps:</p>


<ul>
<li><strong>Getting</strong> the change into a repo (pushing or pulling)</li>
<li><strong>Applying</strong> the change to the files (update or merge)</li>
<li><strong>Saving</strong> the new version (commit)</li>
</ul>



<p>Second, depending on the change, you can update or merge:</p>


<ul>
<li><strong>Updates</strong> happen when there’s no ambiguity. For example, I pull changes to a file you’ve edited all along. The file just jumps to the latest revision.</li>
<li><strong>Merges</strong> are needed when we have conflicting changes. If we start with the same file but make different changes, we end up with two “branches” (i.e. alternate universes). One world has my changes, the other has yours. In this case we (probably) want to merge the changes and unite the universe.</li>
</ul>



<p>I’m still wrapping my head around how easy it is for branches to spring up and collapse in a <span>DVCS</span>:</p>

<p><img src="http://betterexplained.com/wp-content/uploads/version_control/distributed/distributed_merge.png" alt=""></p>

<p>In this case, a merge is needed because (+Soup) and (+Juice) are changes to a common parent: the list with just “Milk”. After Joe merges the files, Sue can do a regular “pull and update” to get the combined file from Joe. She doesn’t have to merge again on her own.</p>

<p>In Mercurial you can run:</p>



<pre>
<code>
hg incoming ../another-dir  (see pending changes)
hg pull ../another-dir      (download changes)

hg update                   (actually apply changes...)
hg merge                    (... or merge if needed)

hg commit                   (check in merged file; unite branches)
</code>
</pre>



<p>Yep, the “pull-merge-commit” cycle is long. Luckily, Mercurial has shortcuts to combine commands into a single one. Though it seems complex, it’s <strong>much</strong> easier than handling merges manually in Subversion.</p>

<p><strong>Most merges are automatic.</strong> When conflicts come up, they are typically resolved quickly. Mercurial keeps track of the parent/child relationship for every change (our merged list has two parents), as well as the “heads” or latest changes in each branch. Before the merge we have two heads; afterwards, one.</p>

<h2>Organizing a Distributed Project</h2>

<p>Here’s one way to organize a distributed project:</p>

<p><img src="http://betterexplained.com/wp-content/uploads/version_control/distributed/distributed_push_pull.png" alt=""></p>

<p>Sue, Joe and Eve check changes into a common branch. They can trade patches with each other to do simple <strong>“buddy builds”</strong>: <em>Hey buddy, can you try out these patches? I need to see if it works before I push to the experimental branch.</em></p>

<p>Later, a maintainer can review and pull changes from the experimental branch into a stable branch, which has the latest release. A distributed <span>VCS </span>helps isolate changes but still provide the “single source” of a centralized one. There are many models of development, from “pull only” (where maintainers decide what to take, and is used when developing Linux) to “shared push” (which acts like a centralized system). A distributed <span>VCS </span>gives you <strong>flexibility</strong> in how a project is maintained.</p>

<h2>Practice And Scathing Ridicule Makes Perfect</h2>

<p>I’m a <span>DVCS </span>newbie, but am happy with what I’ve learned so far. I enjoy <span>SVN, </span>but it’s “fun” seeing how easy a merge can be. My suggestion is to start with Subversion, get a grasp for team collaboration, then experiment with a distributed model. With the proper layout a <span>DVCS </span>can do anything a centralized system can, with the added benefit of easy merging.</p>

<p><strong>Online Resources</strong></p>


<ul>
<li><a href="http://www.selenic.com/mercurial/wiki/">Mercurial</a> has an <a href="http://hgbook.red-bean.com/hgbook.html">excellent book</a>. On Windows you may need <a href="http://kdiff3.sourceforge.net/">diffing/merging software</a> or <a href="http://tortoisesvn.tigris.org/TortoiseMerge.html">TortoiseMerge</a> (if you have TortoiseSVN installed).</li>
<li><a href="http://darcs.net/">Darcs</a> has a detailed <a href="http://en.wikibooks.org/wiki/Understanding_darcs">wikibook</a> (has some math theory about changes).</li>
<li><a href="http://git.or.cz">Git</a> was created by Linus Torvalds. Here’s an interesting lecture on <span>DVCS</span>; prepare to be berated for using a centralized system:</li>
</ul>



<p><embed src="http://www.youtube.com/v/4XpnKHJAok8" width="425" height="350" allowScriptAccess="never" wmode="transparent" type="application/x-shockwave-flash"></embed></p>

<p>Notable Quotes:</p>


<ul>
<li>“How many have done a branch and merged it? How many of you enjoyed it?”</li>
<li>“When you do a merge, you plan ahead for a week, then set aside a day to do it.”</li>
<li>“Some people have 5, 10, 15 branches”. One branch is experimental. One branch is maintenance, etc.</li>
<li>“CVS — you don’t commit. You make changes without committing. You never commit until it passes a giant test suite. People make 1-liner changes, knowing it can’t <em>possibly</em> break.”</li>
</ul>



<p>So good luck, and watch out for the holy wars. Feel free to share any tips or suggestions below. </p><img src="http://feeds.feedburner.com/~r/Betterexplained/~4/170014927" height="1" width="1"></div>
</div>

<div class="feed">
  <div class="title">46. <a href="http://feeds.feedburner.com/~r/Betterexplained/~3/174644290/" target="_blank">Surprising Uses of the Pythagorean Theorem</a></div>
  <div class="content"><p> <img src="http://betterexplained.com/latexrender/pictures/b6dd3bb282beb5c5bd30432d0c8b52e5.gif" title="\displaystyle{a^2 + b^2 = c^2}" alt="\displaystyle{a^2 + b^2 = c^2}"></p>

<p>The Pythagorean theorem is a celebrity: if an equation can make it into <a href="http://www.snpp.com/episodes/1F08.html">the Simpsons</a>, I'd say its well-known.</p>

<p>But most of us think the formula only applies to triangles and geometry. Think again. The Pythagorean Theorem can be used with <strong>any shape</strong> and for <strong>any formula that squares a number</strong>. </p>

<p>Read on to see how this 2000-year-old idea can help us understand computer science, physics, even the value of Web 2.0 social networks.</p>

<h2>Understanding How Area Works</h2>

<p>I love seeing old topics in a new light and discovering the depth there. For example, I realize I didn't have a <strong>deep grasp</strong> of area until writing this article. Yes, we can rattle off equations, but do we really <em>understand</em> the nature of area? This fact may surprise you:</p>

<p>The area of <strong>any shape</strong> can be computed from <strong>any line segment squared</strong>. In a square, our "line segment" is usually a side, and the area is that side squared (side 5, area 25). In a circle, the line segment is often the radius, and the area is pi * r^2 (radius 5, area 25 pi). Easy enough.</p>

<p><strong>We can pick any line segment</strong> and figure out area from it: every line segment has an "area factor" in this universal equation:</p>

<p><img src="http://betterexplained.com/latexrender/pictures/83851b41240613e0739eb46be6649e6c.gif" title="\displaystyle{Area = Factor \cdot (line \hspace{1mm} segment)^2}" alt="\displaystyle{Area = Factor \cdot (line \hspace{1mm} segment)^2}"></p>

<table style="border:1px solid #ccc;text-align:middle" width="100%">
<tr style="border:1px solid #ccc">
	<th><h2>Shape</h2></th>
	<th><h2>Line Segment</h2></th>
	<th><h2>Area</h2></th>
	<th><h2>Area Factor</h2></th>
</tr>
<tr>
	<td>Square<br>
<img src="http://betterexplained.com/wp-content/uploads/pythagorean/square1.png" alt="">
</td>
	<td>Side [s]</td>
	<td>s<sup>2</sup></td>
	<td>1</td>
</tr>
<tr>
	<td>Square<br>
<img src="http://betterexplained.com/wp-content/uploads/pythagorean/square_perimeter.png" alt="">
</td>
	<td>Perimeter [p]</td>
	<td>1/16 p<sup>2</sup></td>
	<td>1/16</td>
</tr>
<tr>
	<td>Square<br>
<img src="http://betterexplained.com/wp-content/uploads/pythagorean/square_diagonal.png" alt="">
</td>
	<td>Diagonal [d]</td>
	<td>1/2 d<sup>2</sup></td>
	<td>1/2</td>
</tr>
<tr>
	<td>Circle<br>
<img src="http://betterexplained.com/wp-content/uploads/pythagorean/circle1.png" alt="">
</td>
	<td>Radius [ r ]</td>
	<td>pi r<sup>2</sup></td>
	<td>pi (3.14159...)</td>
</tr>
</table>

<p>For example, look at the diagonal of a square ("d"). A regular side is d/sqrt(2), so the area becomes 1/2 d<sup>2</sup>. Our "area contant" is 1/2 in this case, if we want to use the diagonal as our line segment to be squared.</p>

<p>Now, use the <strong>entire perimeter ("p")</strong> as the line segment. A side is p/4, so the area is p<sup>2</sup>/16. The area factor is 1/16 if we want to use p<sup>2</sup>.</p>

<h2>Can we pick any line segment? </h2>

<p>You bet. There is always <strong>some</strong> relationship between the "traditional" line segment (the side of a square), and the one you pick (the perimeter, which happens to be 4 times a side). Since we can convert between the "traditional" and "new" segment, it doesn't matter which one we use -- there'll just be a different area factor when we multiply it out.</p>

<h2>Can we pick any shape?</h2>

<p>Sort of. A given area formula works for all <strong>similar</strong> shapes, where "similar" means "zoomed versions of each other". For example:</p>


<ul>
<li>All squares are similar (area always s<sup>2</sup>)</li>
<li>All circles are similar, too (area always pi r<sup>2</sup>)</li>
<li>Triangles are <strong>not similar</strong>: Some are fat and others skinny -- every "type" of triangle has its own area factor based on the line segment you are using. Change the shape of the triange and the equation changes.</li>
</ul>



<p>Yes, every triangle follows the rule "area = 1/2 base * height". But the relationship between base and height depends on the type of triangle (base = 2 * height, base = 3 * height, etc.), so even then the area factor will be different.</p>

<p>Why do we need similar shapes to keep the same area equation? Intuitively, when you zoom (scale) a shape, you're changing the absolute size but not the relative ratios within the shape. A square, no matter how zoomed, has a perimeter = 4 * side.</p>

<p>Because the "area factor" is based on ratios inside the shape, any shapes with the same "ratios" will follow the same formula. It's a bit like saying everyone's armspan is about equal to their height. No matter if you're a <span>NBA </span>basketball player or child, the equation holds because it's all relative. (This intuitive argument may not satisfy a mathematical mind -- Wikipedia has details on similarity).</p>

<p>I hope these high-level concepts make sense:</p>


<ul>
<li>Area can be be found from <strong>any line segment squared</strong>, not just the "side" or "radius"</li>
<li>Each line segment has a different "area factor"</li>
<li>The same area equation works for similar shapes</li>
</ul>



<h2>Intuitive Look at The Pythagorean Theorem</h2>

<p>We can all agree the Pythagorean Theorem is true (here's <a href="http://www.cut-the-knot.org/pythagoras/index.shtml">75 proofs</a>). But most proofs offer a mechanical understanding: re-arrange the shapes, and voila, the equation holds. But is it really clear, intuitively, that it <strong>must</strong> be a<sup>2</sup> + b<sup>2</sup> = c<sup>2</sup> and not 2a<sup>2</sup> + b<sup>2</sup> = c<sup>2</sup>? No? Well, let's build some intuition.</p>

<p>There's one killer concept we need: <strong>Any right triangle can be split into two similar right triangles.</strong></p>

<p><img src="http://betterexplained.com/wp-content/uploads/pythagorean/proof1.png" alt=""></p>

<p>Cool, huh? Drawing a perpendicular line through the point splits a right triangle into two smaller ones. Geometry lovers, try the proof yourself: use angle-angle-angle similarity.</p>

<p>This diagram also makes something very clear:</p>


<ul>
<li>Area (Big) = Area (Medium) + Area (Small)</li>
</ul>



<p>Makes sense, right? The smaller trianges were cut from the big one, so the areas must add up. And the kicker: because the <strong>triangles are similar, they have the same area equation.</strong></p>

<p>Let's call the long side c (5), the middle side b (4), and the small side a (3). Our area equation for these triangles is:</p>

<p><img src="http://betterexplained.com/latexrender/pictures/f7bbcef0aec502895dbab91447d42577.gif" title="\displaystyle{Area = F * hypotenuse^2}" alt="\displaystyle{Area = F * hypotenuse^2}"></p>

<p>where F is some area factor (6/25 or .24 in this case; the exact number doesn't matter). Now let's play with the equation:</p>

<p><img src="http://betterexplained.com/latexrender/pictures/45bc247dd004f7ff5dffd5679f01b5ab.gif" title="\displaystyle{Area (Big) = Area (Medium) + Area (Small)}" alt="\displaystyle{Area (Big) = Area (Medium) + Area (Small)}"></p>

<p><img src="http://betterexplained.com/latexrender/pictures/db37b4088392174c977843b9dfb3394b.gif" title="\displaystyle{F c^2 = F b^2 + F a^2}" alt="\displaystyle{F c^2 = F b^2 + F a^2}"></p>

<p>Divide by F on both sides and you get:</p>

<p><img src="http://betterexplained.com/latexrender/pictures/956b37ed3b28bf830792b33c81c5b918.gif" title="\displaystyle{c^2 = b^2 + a^2}" alt="\displaystyle{c^2 = b^2 + a^2}"></p>

<p>Which is our famous theorem! You knew it was true, but now you <strong>know why</strong>:</p>


<ul>
<li>A triangle can be split into two smaller, similar ones</li>
<li>Since the areas must add up, the squared hypotenuses (which determine area) must add up as well.</li>
</ul>



<p>This takes a bit of time to see, but I hope the result is clear. How could the small triangles <strong>not</strong> add to the larger one? </p>

<p>Actually, it turns out the Pythagorean Theorem depends on the assumptions of Euclidean geometry and doesn't work on spheres or globes, for example. But we'll save that discussion for another time.</p>

<h2>Useful Application: Try Any Shape</h2>

<p>We used triangles in our diagram, the simplest 2-D shape. But the line segment can belong to <strong>any</strong> shape. Take circles, for example:</p>

<p><img src="http://betterexplained.com/wp-content/uploads/pythagorean/example1.png" alt=""></p>

<p>Now what happens when we add them together?</p>

<p><img src="http://betterexplained.com/wp-content/uploads/pythagorean/example_circles.png" alt=""></p>

<p>You guessed it: Circle of radius 5 = Circle of radius 4 + Circle of radius 3. </p>

<p>Pretty wild, eh? We can multiply the Pythagorean Theorem by our area factor (pi, in this case) and come up with a relationship for any shape.</p>

<p>Remember, the line segment can be <strong>any portion of the shape</strong>. We could have picked the circle's radius, diamater, or circumference -- there would be a different area factor, but the 3-4-5 relationship would still hold.</p>

<p>So, whether you're adding up pizzas or Richard Nixon masks, the Pythagorean theorem helps you relate the areas of any similar shapes. Now that's something they didn't teach you in grade school.</p>

<h2>Useful Application: Conservation of Squares</h2>

<p>The Pythagorean Theorem applies to <strong>any</strong> equation that has a square. The triangle-splitting means you can split any amount (c<sup>2</sup>) into two smaller amounts (a<sup>2</sup> + b<sup>2</sup>) based on the sides of a right triangle. In reality, the "length" of a side can be distance, energy, work, time, or even people in a social network:</p>

<p><strong>Social Networks.</strong> </p>

<p><a href="http://en.wikipedia.org/wiki/Metcalfe&#39;s_law">Metcalfe's Law</a> (if you believe it) says the value of a network is about n<sup>2</sup> (the number of relationships). In terms of value,</p>


<ul>
<li>Network of 50M  = Network of 40M + Network of 30M.</li>
</ul>



<p>Pretty amazing -- the 2nd and 3rd networks have 70M people total, but they aren't a coherent whole. The network with 50 million people is as valuable as the others combined.</p>

<p><strong>Computer Science</strong></p>

<p>Some programs with n inputs take n<sup>2</sup> time to run (bubble sort, for example). In terms of processing time:</p>


<ul>
<li>50 inputs = 40 inputs + 30 inputs</li>
</ul>



<p>Pretty interesting. 70 elements spread among two groups can be sorted as fast as 50 items in one group. (Yeah, there may be constant overhead/start up time, just work with me here).</p>

<p>Given this relationship, it makes sense to partition elements into separate groups and then sort the subgroups. Indeed, that's the approach used in quicksort, one of the best general-purpose sorting methods. The Pythagorean theorem helps show how sorting 50 combined elements can be as slow as sorting 30 and 40 separate ones.</p>

<p><strong>Surface Area</strong></p>

<p>The surface area of a sphere is 4 pi r<sup>2</sup>. So, in terms of surface area of spheres:</p>


<ul>
<li>Area of radius 50 = area of radius 40 + area of radius 30</li>
</ul>



<p>We don't often have spheres lying around, but boat hulls may have the same relationship (they're like deformed spheres, right?). Assuming the boats are similarly shaped, the paint needed to coat one 50 foot yacht could instead paint a 40 and 30-footer. Yowza.</p>

<p><strong>Physics</strong></p>

<p>If you remember your old physics classes, the kinetic energy of an object with mass m and velocity v is <strong>1/2 m v<sup>2</sup></strong>. In terms of energy,</p>


<ul>
<li>Energy at 500 mph = Energy at 400 mph + Energy at 300 mph</li>
</ul>



<p>With the energy used to accelerate one bullet to 500 mph, we could accelerate two others to 400 and 300 mph.</p>

<h2>Try Any Number</h2>

<p>You can use any set of numbers that make a right triangle. For example, enter a total amount (50) and one subportion (30), and the remainder will appear below:</p>

<p></p>

<p>Suppose you want to see if a large pizza (16 inches) is bigger than two mediums (12 inches). Plug in 16 for C, and 12 for A. It looks like the large pizza can be split into a 12-inch and 10.5-inch pizza, so two-mediums are in fact bigger.</p>

<h2>Enjoy Your New Insight</h2>

<p>Throughout our school life we think the Pythagorean Theorem is about triangles and geometry. <strong>It's not.</strong></p>

<p>When you see a right triangle, realize the sides can represent the lengths of any portion of a shape, and the sides can represent variables in <strong>any equation</strong> that has a square. Maybe it's just me, but I find this pretty surprising.</p>

<p>There's much, much more to this beautiful theorem, such as <a href="http://betterexplained.com/articles/measure-any-distance-with-the-pythagorean-theorem/">measuring any distance</a>. Enjoy. </p><img src="http://feeds.feedburner.com/~r/Betterexplained/~4/174644290" height="1" width="1"></div>
</div>

<div class="feed">
  <div class="title">47. <a href="http://feeds.feedburner.com/~r/Betterexplained/~3/179941836/" target="_blank">How To Measure Any Distance With The Pythagorean Theorem</a></div>
  <div class="content"><p> We’ve underestimated the Pythagorean theorem all along. It’s not about triangles; <a href="http://betterexplained.com/articles/surprising-uses-of-the-pythagorean-theorem/">it can apply to any shape</a>. It’s not about a, b and c; it applies to <strong>any formula</strong> with a squared term.</p>

<p>It’s not about <strong>distance</strong> in the sense of walking diagonally across a room. It’s about <strong>any distance</strong>, like the “distance” between our movie preferences or colors. </p>

<p>If it can be measured, it can be compared with the Pythagorean Theorem. Let’s see why.</p>

<h2>Understanding The Theorem</h2>

<p>We agree the theorem works. In any right triangle:</p>

<p><img src="http://betterexplained.com/wp-content/uploads/pythagorean/theorem.png" alt=""></p>

<p>If a=3 and b=4, then c=5. Easy, right?</p>

<p>Well, a <strong>key observation</strong> is that a and b are at right angles (notice the little red box). Movement in one direction has <strong>no impact</strong> on the other.</p>

<p>It’s a bit like North/South vs. East/West. Moving North does not change your East/West direction, and vice-versa — the directions are independent (the geek term is <strong>orthogonal</strong>).</p>

<p>The Pythagorean Theorem lets you use find the <strong>shortest path distance</strong> between orthogonal directions. So it’s not really about right <strong>triangles</strong> — it’s about comparing “things” moving at right angles.</p>

<blockquote>
You: <em>If I walk 3 blocks East and 4 blocks North, how far am I from my starting point?</em><br>
Me: <em>5 blocks, as the crow flies. Be sure to bring adequate provisions for your journey.</em><br>
You: <em>Uh, ok.</em><br>
</blockquote>

<h2>So what is “c”?</h2>

<p>Well, we could think of c as just a number, but that keeps us in boring triangle-land. I like to think of c as a <strong>combination of a and b</strong>.</p>

<p>But it’s not a simple combination like addition — after all, c doesn’t equal a + b. It’s more a combination of components — the Pythagorean theorem lets us combine <strong>orthogonal components</strong> in a manner similar to addition. And there’s the magic.</p>

<h2>Chaining the Theorem</h2>

<p>Let’s get crazy and chain the theorem together. Take a look at this:</p>

<p><img src="http://betterexplained.com/wp-content/uploads/pythagorean/chainedtheorem.png" alt=""></p>

<p>Cool, eh? We draw <strong>another</strong> triangle in red, using c as one of the sides. Since c and d are at right angles (orthogonal!), we get the Pythagorean relation: c<sup>2</sup> + d<sup>2</sup> = e<sup>2</sup>.</p>

<p>And when we replace c<sup>2</sup> with a<sup>2</sup> + b<sup>2</sup> we get:</p>

<p><img src="http://betterexplained.com/latexrender/pictures/731fcc3b4fdc8e20ea821d1bb3f2971f.gif" title="\displaystyle{a^2 + b^2 + d^2 = e^2}" alt="\displaystyle{a^2 + b^2 + d^2 = e^2}"></p>

<p>And that’s something: We’ve written e in terms of 3 orthogonal components (a, b and d). Starting to see a pattern?</p>

<h2>Put on your 3D Goggles</h2>

<p>Think two triangles are strange? Try pulling one out of the paper. Instead of lining the triangles flat, tilt the red one up:</p>

<p><img src="http://betterexplained.com/wp-content/uploads/pythagorean/3dtheorem.png" alt=""></p>

<p>It’s the same triangle, just facing a different way. But now we’re in 3d! If we call the sides x, y and z instead of a, b and d we get:</p>

<p><img src="http://betterexplained.com/latexrender/pictures/ba69532eab0d49814af6c352916f9761.gif" title="\displaystyle{x^2 + y^2 + z^2 = distance^2}" alt="\displaystyle{x^2 + y^2 + z^2 = distance^2}"></p>

<p>Very nice. In math we typically measure the x-coordinate [left/right distance], the y-coordinate [front-back distance], and the z-coordinate [up/down distance]. And now we can find the 3-d distance to a point given its coordinates!</p>

<h2>Use Any Number of Dimensions</h2>

<p>As you can guess, the Pythagorean Theorem generalizes to <strong>any number of dimensions</strong>. That is, you can chain a bunch triangles together and tally up the “outside” sections:</p>

<p><img src="http://betterexplained.com/wp-content/uploads/pythagorean/multichain.png" alt=""></p>

<p>You can imagine that each triangle is in its own dimension. If segments are at right angles, the theorem holds and the math works out.</p>

<h2>How Distance Is Computed</h2>

<p>The Pythagorean Theorem is the basis for computing distance between two points. Consider two triangles:</p>


<ul>
<li>Triangle with sides (4,3) [blue]</li>
<li>Triangle with sides (8,5) [pink]</li>
</ul>



<p><img src="http://betterexplained.com/wp-content/uploads/pythagorean/distance.png" alt=""></p>

<p>What’s the distance from the tip of the blue triangle [at coordinates (4,3)] tot the tip of the red triangle [at coordinates (8,5)]? Well, we can create a <strong>virtual triangle</strong> between the endpoints by subtracting corresponding sides. The hypotenuse of the virtual triangle is the distance between points:</p>


<ul>
<li>Distance: (8-4,5-3) = (4,2) = sqrt(20) = 4.47</li>
</ul>



<p>Cool, eh? In 3D, we can find the distance between points (x1,y1,z1) and (x2,y2,z2) using the same approach:</p>

<p><img src="http://betterexplained.com/latexrender/pictures/d322f01b296c4444f1c4a1713d8c2cfb.gif" title="\displaystyle{distance^2 = (x_2 - x_1)^2 + (y_2 - y_1)^2 + (z_2 - z_1)^2}" alt="\displaystyle{distance^2 = (x_2 - x_1)^2 + (y_2 - y_1)^2 + (z_2 - z_1)^2}"></p>

<p>And it doesn’t matter if one side is bigger than the other, since the difference is squared and will be positive (another great side-effect of the theorem).</p>

<h2>How to Use Any Distance</h2>

<p>The theorem isn’t limited to our narrow, spatial definition of distance. It can apply to <strong>any orthogonal dimensions</strong>: space, time, movie tastes, colors, temperatures. In fact, it can apply to any set of numbers (a,b,c,d,e). Let’s take a look.</p>

<h2>Measuring User Preferences</h2>

<p>Let’s say you do a survey to find movie preferences:</p>

<p>1. How did you like Rambo? (1-10)<br>
2. How did you like Bambi? (1-10)<br>
3. How did you like Seinfeld? (1-10)</p>

<p>How do we compare people’s ratings? Find similar preferences? Pythagoras to the rescue! </p>

<p>If we represent ratings as a “point” (Rambo, Bambi, Seinfeld) we can represent our survey responses like this:</p>


<ul>
<li>Tough Guy: (10, 1, 3)</li>
<li>Average Joe: (5, 5, 5)</li>
<li>Sensitive Guy: (1, 10, 7)</li>
</ul>



<p>And using the theorem, we can see how “different” people are:</p>


<ul>
<li>Tough Guy to Average Joe: (10 - 5, 1 - 5, 3 - 5) = (5, -4, -2) = 6.7</li>
<li>Tough Guy to Sensitive Guy: (10 - 1, 1 - 10, 3 - 7) = (9, -9, -4) = 13.34</li>
</ul>



<p>As we suspected, there’s a large gap between the Tough and Sensitive Guy, with Average Joe in the middle. The theorem helps us <strong>quantify this distance</strong> and do interesting things like <strong>cluster similar results</strong>.</p>

<p>This technique can be used to rate <a href="http://www.netflixprize.com/">Netflix movie preferences</a> and other types of <strong>collaborative filtering</strong> where you attempt to make predictions based on preferences (i.e. Amazon recommendations). In geek speak, we represented preferences as a vector, and use the theorem to find the distance between them (and group similar items, perhaps).</p>

<h2>Finding Color Distance</h2>

<p>Measuring “distance” between colors is another useful application. Colors are represented as red/green/blue (RGB) values from 0(min) to 255 (max). For example</p>


<ul>
<li>Black: (0, 0, 0) — no colors</li>
<li>White: (255, 255, 255) — maximum of each color</li>
<li>Red: (255, 0, 0) — pure red, no other colors</li>
</ul>



<p>We can map out all colors in a “color space”, <a href="http://en.wikipedia.org/wiki/RGB_color_model">like so</a>:</p>

<p><img src="http://betterexplained.com/wp-content/uploads/pythagorean/colorcube.png" alt=""></p>

<p>We can get distance between colors the usual way: get the distance from our (red, green, blue) value to black (0,0,0) [formally labeled <a href="http://colorspan.com/support/tools/deltae.asp">delta e</a>]. It appears humans can’t tell the difference between colors only 4 units apart; heck, even 30 units looks pretty close to me:</p>

<p><img src="http://betterexplained.com/wp-content/uploads/pythagorean/colordistance.png" alt=""></p>

<p>How similar do these look to you? The color distance gives us a <strong>quantifiable</strong> way to measure the distance between colors (<a href="http://instacalc.com/?d=Q2FsY3VsYXRlIHRoZSBkaXN0YW5jZSBvZiBhIHBhcnRpY3VsYXIgUkdCIHZhbHVlIGZyb20gYmxhY2sgKDAsMCwwKS4&amp;%23038;c=cmVkID0gMTAwfGdyZWVuID0gMTAwfGJsdWUgPSAxMDB8ZGlzdCA9IHNxcnQocmVkXjIgKyBncmVlbl4yICsgYmx1ZV4yKSAvLyBkaXN0YW5jZSBmcm9tIGJsYWNrfHx8&amp;%23038;s=sssssss&amp;%23038;v=0.9">try for yourself</a>). You can even <a href="http://dheera.net/projects/blur.php">unscramble certain blurred images</a> by cleverly applying color distance.</p>

<h2>The Point: You can measure anything</h2>

<p>If you can represent a set of characteristics with numbers, you can compare them with the theorem:</p>


<ul>
<li>Temperatures during the week: (Mon, Tues, Wed, Thurs, Fri). Compare successive weeks to see how “different” they are (find the difference between 5-dimensional vectors).</li>
<li>Number of customers coming into a store hour-by-hour, day-by-day, or week-by-week</li>
<li>SpaceTime distance: (latitude, longitude, altitude, date). Useful if you’re making a time machine (or a video game that uses one)!</li>
<li>Differences between people: (Height, Weight, Age)</li>
<li>Differences between companies: (Revenue, Profit, Market Cap)</li>
</ul>



<p>You can tweak the distance by weighing traits differently (i.e., multiplying the age difference by a certain factor). But the core idea is so important I’ll repeat it again: <strong>if you can quantify it, you can compare it using the the Pythagorean Theorem.</strong></p>

<p>Your x, y and z axes can represent any quantity. And you aren’t limited to 3 dimensions. Sure, mathematicians would love to tell you about the other ways to measure distance (aka <a href="http://en.wikipedia.org/wiki/Metric_space">metric space</a>), but the Pythagorean Theorem is the most famous and a great starting point.</p>

<h2>So, What Just Happened Here?</h2>

<p>There’s so much to learn when revisiting concepts we were “taught”. Math is beautiful, but the elegance is usually buried under mechanical proofs and a wall of equations. We don’t need more proofs; we need interesting, intuitive results. </p>

<p>For example, the Pythagorean Theorem:</p>


<ul>
<li>Works for <strong>any shape</strong>, not just triangles (like circles)</li>
<li>Works for <strong>any equation with squares</strong> (like 1/2 m v<sup>2</sup>)</li>
<li>Generalizes to <strong>any number of dimensions</strong> (a<sup>2</sup> + b<sup>2</sup> + c<sup>2</sup> + …)</li>
<li>Measures <strong>any type of distance</strong> (i.e. between colors or movie preferences)</li>
</ul>



<p>Not too bad for a 2000-year old result, right? This is quite a brainful, so I’ll finish here for today (the <a href="http://betterexplained.com/articles/surprising-uses-of-the-pythagorean-theorem/">previous article</a> has more uses). Happy math. </p><img src="http://feeds.feedburner.com/~r/Betterexplained/~4/179941836" height="1" width="1"></div>
</div>

<div class="feed">
  <div class="title">48. <a href="http://feeds.feedburner.com/~r/Betterexplained/~3/185024295/" target="_blank">What You Should Know About The Stock Market</a></div>
  <div class="content"><p> Everyone’s heard of the stock market — but few know <strong>why</strong> it works. Were you aware that each stock has two prices? That you can’t buy and sell for the same amount? That a “stock market” works better and is more open than a “stock store”?</p>

<p>If you’re like most of us, probably not. Here’s why stock markets rock:</p>


<ul>
<li>They <strong>match buyers and sellers efficiently</strong></li>
<li>All prices are <strong>completely transparent</strong> and you see what other people have paid/sold for</li>
<li>You <strong>pick your own price</strong> and will get that amount if there’s a willing partner</li>
</ul>



<p>Most explanations bodyslam you into a tarpit of history and minor details. Not here. Let’s see <strong>why</strong> the stock market works; most things have a reason, if you look hard enough.</p>

<h2>iPods Ahoy!</h2>

<p>Focus groups informed me that iPods are “hip” in the 18-35 demographic. They want me to find the price, so I’ll ask you:</p>

<blockquote>
Me: You, the 18-35 year old demographic, want an iPod. What’s it worth?<br>
You: <strong>Get the price. Duh.</strong><br>
</blockquote>

<p>Ok hotshot, riddle me this: what <em>is</em> the price, exactly?</p>


<ul>
<li>What you can <strong>buy it for</strong>? (Your best <strong>bid</strong>)</li>
<li>What you can <strong>sell it for</strong>? (What you’d <strong>ask</strong> for it)</li>
</ul>



<p>So which price is the “real one”? Both.</p>

<p>You see, buyers and sellers each have prices in mind. When prices match, <strong>whablamo</strong>, there’s a transaction (no match, <em>no whablamo</em>).</p>

<p>The idea of <strong>two prices for every item</strong> is key to understanding any market, not just stocks. Everything has a bid and an ask, and each shopping model has a different way of handling them. This leads to different advantages for buyers and sellers.</p>

<h2>Shopping Time</h2>

<p>Suppose we want to buy an iPod from Amazon. You see the selling price of $200 (Amazon’s ask), and personally decide if it’s “worth it” (i.e. less than or equal to your bid):</p>

<p><img src="http://betterexplained.com/wp-content/uploads/stock/store-model.png"></p>

<p>In the store model, Amazon shows a <strong>public asking price ($200)</strong>. Each buyer has a <strong>secret bidding price</strong>, some more than others. Buyers willing to bid $200 or more purchase the iPod; the rest hold off ($199 and below).</p>

<p>Amazon picks a price that attracts the most bidders yet still keeps a profit. In the <strong>store model</strong>:</p>


<ul>
<li>Buyer pro: Buyers know the price and can pay less than their internal value</li>
<li>Buyer con: Buyers have to visit multiple stores</li>
<li>Seller con: Can’t see what each buyer is willing to pay; difficult to set pricing. Do low sales mean a bad price, or bad product?</li>
</ul>



<p>Even though buyers are “in control”, they may have to search around to find a store that meets their bid (if any). That’s inefficient.</p>

<h2>Onto eBay</h2>

<p>Now suppose we want to sell our new, unopened gadget (you, the 18-35 demographic, are fickle like that; the survey said so). Sure, we could try to sell it on Amazon — now we’re our own store and need a price we <strong>think</strong> people will pay. We’re in the same boat as Amazon, and could set the price too low. That’s no fun.</p>

<p>Instead, we auction off the new iPod on eBay to maximize profits:</p>

<p><img src="http://betterexplained.com/wp-content/uploads/stock/ebay-model.png"></p>

<p>In the eBay model, buyers have <strong>public bids</strong> and compete for the product. The seller keeps their minimum price secret and hopes to make a profit by having someone “overpay”. In the <strong>auction model</strong>:</p>


<ul>
<li>Seller pro: Sellers have a <strong>secret ask</strong> (reserve or minimum price) and can get paid above this.</li>
<li>Seller pro: Buyers’ demand is transparent. They can easily see if they are pricing too high.</li>
<li>Buyer con: Difficult to buy a product.</li>
</ul>



<p>eBay is great for sellers — you have the chance of making extra profit. For buyers, it’s not so great: you can lose auctions by $1 (paying 201 when 202 was the highest bid), even though the seller would have been happy with 201. You could enter multiple auctions with $201 but risk getting two iPods.</p>

<h2>Want Ads and Hagglers</h2>

<p>There’s other trading approaches also:</p>


<ul>
<li><strong>Want ad</strong>: Publicly announce your desire for an iPod and let sellers fight it out.</li>
<li><strong>Haggle</strong>: Find someone with an iPod, and without knowing a selling price, make an offer. You both haggle back and forth, trying to eke the other person out of a few bucks. If you’ve gone car shopping you know how fun this is.</li>
</ul>



<p>In want ads, the asks are transparent while the bids (your value) are hidden. When haggling, both prices are hidden which can lead to a stressful situation.</p>

<h2>It’s About Supply and Demand</h2>

<p>Each model has similar concepts, namely:</p>


<ul>
<li>Supply: sellers provide <strong>asks</strong></li>
<li>Demand: buyers provide <strong>bids</strong></li>
</ul>



<p>The phrase <strong>liquidity</strong> refers to how effectively you can trade; how easily cash can <strong>flow</strong>. When buyers and sellers have to argue or haggle, trading freezes up. In particular, there’s a common problem in the market above:</p>


<ul>
<li>There’s secret prices and a <strong>lack of transparency</strong></li>
<li>There’s multiple vendors and a <strong>lack of consolidation</strong></li>
</ul>



<p>When buyers and sellers need to search to find each other, and haggle when they get there, trading slows down.</p>

<h2>Enter the Market</h2>

<p>But hope is not lost! Surprisingly, the very symbol of capitalism is an “open source” model:</p>


<ul>
<li>All <strong>prices are transparent</strong></li>
<li>Buyers <strong>write public bids</strong> (buying price)</li>
<li>Sellers <strong>write public asks</strong> (selling price)</li>
<li>There’s <strong>one location</strong> to get a particular stock; there’s no searching</li>
<li><strong>Dealers/specialists</strong> help match buyers and sellers</li>
</ul>



<p>And here’s what it looks like:</p>

<p><img src="http://betterexplained.com/wp-content/uploads/stock/market-model.png"></p>

<p>Every iPod seller lists their asking price (210, 205, 201, 200). Every iPod buyer lists their buying price (190, 195, 199, 200). When prices match, a transaction happens: the buyer who wants to pay 200 gets matched with the seller who wants 200. They’re happy.</p>

<p>Eventually the matches cease and we come to a standstill.</p>

<h2>Drop and Spread ‘em.</h2>

<p>Trades don’t last forever: there’s a standoff and an awkward pause. The lowest sellers want $201, and the highest bidder wants $199; this $2 gap is called <strong>the spread</strong>. The <strong>last price</strong> of a transaction was $200.</p>

<p>Now what happens? Buyers and sellers can do:</p>


<ul>
<li><strong>Limit order:</strong> put their bid/ask in the queue.</li>
<li><strong>Market order:</strong> buy or sell <strong>immediately</strong>.</li>
</ul>



<p>When you place a limit order (”Buy an iPod for 195″), your order gets added to the bid queue (similar for asks). </p>

<p>If you need to trade <strong>right now</strong> (”buy it now!” or “sell it now!”), then you use a market order. You’ll get the best price available:</p>


<ul>
<li>Market order to sell: You can unload your iPod for $199 (the highest bid). The “last” price is now 199.</li>
<li>Market order to buy: You can buy for $201 (the lowest price). The “last” price is now 201.</li>
</ul>



<p>Now this is interesting. Notice how market orders <strong>take items off the queue</strong> and <strong>change the last price</strong>. When people place market orders, the stock price fluctuates. Yes, it’s “just” supply and demand, but it’s pretty cool to know it’s happening real-time in the stock market.</p>

<p>If there’s a lot of buyers, they’ll “use up” the ask queue and the price will rise. If there’s a lot of sellers, they’ll “use up” the bid queue and the price will fall.</p>

<p>This explains why it’s hard to buy and sell for the same price. If you buy for 201, and no new bids come in, you’ll only be able to sell for 199.</p>

<h2>So Who Runs This Popsicle Stand?</h2>

<p>The <span>NYSE </span>and <span>NASDAQ </span>are the two major American exchanges. There are differences, but at the core they provide:</p>


<ul>
<li>A <strong>single market</strong> to trade. All stocks for Microsoft (MSFT), are traded on the <span>NASDAQ </span>exchange. All stocks for Ford (F) are on the <span>NYSE.</span></li>
<li>A <strong>market maker or “specialist”</strong> (not the kind that <a href="http://en.wikipedia.org/wiki/The_Specialist">kills people</a>). These people make the market liquid: they help collect and match bids and asks. The <span>NYSE </span>has one specialst per stock; <span>NASDAQ </span>has several market makers (dealers) who compete on price.</li>
</ul>



<h2>How Do They Make Money?</h2>

<p>Well, often they don’t. In the <span>NYSE,</span> 88% of the trades happen between the public without needing the specialist (remember those guys waving papers and screaming at each other? I wouldn’t want to get involved with them either).</p>

<p>But sometimes they are needed. The market makers literally “create a market” by providing liquidity: you can buy and sell stocks to them at the bid and ask prices. Popular stocks have a small spread due to the demand and volume.</p>

<p>But how do market makers make money? </p>

<p><img src="http://betterexplained.com/wp-content/uploads/stock/market-maker.png"></p>

<p>Well, it’s a bit like a currency exchange at a bank, where’s there’s a different rate for buying and selling. Let’s say Sue has an iPod to sell, and Bob wants to buy an iPod. It might go like this:</p>


<ul>
<li>Hey Sue, I’ll take your iPod. Here’s 199.</li>
<li>Hi Bob, I’ll sell you an iPod. That’ll be 201.</li>
</ul>



<p>See what happened? The market maker bought an iPod for 199 and sold it for 201: it pocketed the spread of $2. Dealers constantly change their prices based on the bids and asks; they can even lose money depending on the trades coming in. But usually it’s a pretty good gig.</p>

<p>You, the investor, can avoid paying “the spread” by placing limit orders to sell or buy at a certain price. But then you aren’t guaranteed to make a trade.</p>

<h2>It’s All About Timing</h2>

<p>Bill Gates has a <a href="http://finance.yahoo.com/q/mh?s=MSFT">lot of shares</a> of Microsoft. People naively put this wealth as “shares times price”, but you know that doesn’t really work. If he tried to sell all his shares, he’d use up the bids. </p>

<p>Each block of shares would be sold for a lower and lower value — and potential buyers would panic and reduce their bids, thinking something was amiss. Sellers would fear the worst and lower their asks to compete. Pandemonium would ensue. So the actual liquidation value of his shares is really some fraction of the reported amount. But it’s still nothing to sneeze at.</p>

<p>Similarly, large institutions must spread their stock trades over time so they don’t disrupt the market (and evaporate their profits). </p>

<p>The market has built-in shock absorbers: as you sell more, the price you get is smaller and smaller, so you sell less. As you buy more, the price you pay gets higher and higher, so you buy less. So it makes sense to take things slow. Nifty.</p>

<h2>There’s Much to Learn</h2>

<p>I’ve simplified a lot of things and only scratched the glossed-over surface. Each market has its own rules to create a trading-friendly environment. Read more here:</p>


<ul>
<li>Invest-faq on the <a href="http://invest-faq.com/articles/exch-nasdaq.html"><span>NASDAQ</span></a> and <a href="http://invest-faq.com/articles/exch-nyse.html"><span>NYSE</span></a>. The <span>NYSE </span>is an “auction market” where bids and asks are public (this is different from eBay auctions, where only bidders compete in a given auction). The <span>NASDAQ </span>is a “dealer market” where you buy/sell from a dealer’s personal inventory.</li>
<li>Investopedia on the <a href="http://www.investopedia.com/ask/answers/128.asp">difference between a market maker and specialist</a></li>
<li>See the current bid/ask for <a href="http://finance.yahoo.com/q?s=MSFT">Microsoft</a> or <a href="http://finance.yahoo.com/q?s=GOOG">Google</a> (and # of shares at that price)</li>
</ul>



<p>But, my goal wasn’t to fill your head with details. I want to share <strong>insight</strong>:</p>


<ul>
<li>Markets exist to <strong>match supply and demand</strong> </li>
<li>The stock market is fast, transparent, and efficient</li>
<li>Every stock has a bid and ask</li>
<li>Buying or selling changes the trading price in a direct, measurable way</li>
</ul>



<p>Want a stock tip? Don’t listen to stock tips. (Stolen from a Charles Schwab ad). This article is about looking at a system as <strong>one way</strong> to solve a larger problem. Happy investing. </p><img src="http://feeds.feedburner.com/~r/Betterexplained/~4/185024295" height="1" width="1"></div>
</div>

<div class="feed">
  <div class="title">49. <a href="http://feeds.feedburner.com/~r/Betterexplained/~3/188134911/" target="_blank">Understanding Accounting Basics (ALOE and Balance Sheets)</a></div>
  <div class="content"><p>In accounting, the math usually isn’t worse than multiplication. But accounting isn’t about math — it’s about <strong>concepts</strong>, and some had me confused. Accounting has simple and surprisingly elegant ways to track a business.</p> <h2>So What’s Accounting About, Anyway?</h2> <p>To be blunt, <strong>accounting is about tracking stuff </strong>(yes, there’s more to it, but hang with me). What kind of stuff can we track?</p> <ul> <li><strong>Assets:</strong> “Stuff” inside the company  <li><strong>Liabilities:</strong> “Stuff” that belongs to others  <li><strong>Owner’s Equity (aka Capital)</strong>: “Stuff” that belongs to the owners </li></li></li></ul> <p>Simple enough. Now how are these related?</p> <h2>Assets = Liabilities + Owner’s Equity</h2> <p>In layman’s terms, <strong>everything the company has belongs to the owners or someone else. </strong>Think of the equation like this:</p> <ul> <li>assets = liabilities + owner’s equity  <li>stuff the company has = other people’s stuff + owner’s stuff </li></li></ul> <p>This formula (also called <span>ALOE</span>) might seem strange at first. Why do we <strong>add</strong> liabilities? Because we’re looking from the point of view of the <strong>company</strong>, not the shareholders. If the company has something, it could be owed to someone else.</p> <p>From the owner’s point of view, owner’s equity = assets - liabilities. This equation looks more natural, but often we aren’t interested in the owner’s point of view. We want to know about the company.</p> <h2>What’s a balance sheet?</h2> <p>A balance sheet is a document that tracks a company’s assets, liabilities and owner’s equity at a specific point in time. As you know, if the company’s has something, it belongs to <em>someone</em>. The sides must balance. So let’s do an example.</p> <p>Suppose we start a company with $100 cash:</p>

<pre><code>Assets:
  Cash: 100 
Liabilities:
  None
Owner's Equity:
  Stock: 100
</code></pre>


<p>The company has $100 in short-term investments, and the owners have $100 worth of stock (how ownership is represented in a company).</p>
<p>Now suppose we take a bank loan for $150. The balance sheet becomes this: 

<pre><code>
Assets:
  Cash: 250 
Liabilities:
  Loans: 150
Owner's Equity
  Stock: 100
</code></pre>


<p></p>
<p>Now our company has $250, but $150 belongs to the bank and $100 belongs to the owners. Sorry guys — you can’t take out a loan and make your share of the company more valuable.</p>
<p>Next, let’s buy a building for $200:

<pre><code>
Assets:
  Cash: 50
  Building: 200
Liabilities:
  Loans: 150
Owner's Equity
  Stock: 100
</code>
</pre>



<p>Buying a building doesn’t make our company more valuable: we re-arranged our assets. Instead of $250 in cash, we have $50 in cash and $200 in “building”. Our share of the company ($100) didn’t change a lick. And we still owe the bank $150.</p>

<h2>That’s not how it really works, is it?</h2>
<p>It is. Well, real accountants use fancier terms (”accounts receivable” vs “deadbeats who owe me”), and have a bigger, badder balance sheet. But the core idea is the same: show what the company’s worth, and who owns what.</p>
<p>Take a look at the balance sheet for <a href="http://finance.yahoo.com/q/bs?s=GOOG">a small internet company</a>:</p>
<p><img src="http://betterexplained.com/wp-content/uploads/accounting/balanceSheet1.png"> </p>
<p>Assets are broken into short-and long-term categories; the company is worth about $18 billion on the books (as of Dec 2006). This is up from $10B in 2005.</p>
<p>There’s many, many reasons why assets may be over or under-valued on the books. How do you measure momentum? Employee morale? A brand? Customer loyalty?</p>
<p>Accountants try to quantify items like this with intangible terms like “Goodwill”, but it’s not easy. In reality, most companies are worth several times their reported assets; Google’s market cap is over 10x the book value (but <a href="http://betterexplained.com/articles/what-you-should-know-about-the-stock-market/">read more about stocks</a> to see why market cap is not quite right).</p>
<p>Now examine the other side of the equation, liabilities and owner’s equity:</p>
<p><img src="http://betterexplained.com/wp-content/uploads/accounting/balanceSheet2.png"></p>
<p>Wow — Google doesn’t have many liabilities! Only $1.4B (of the total $18B) and there’s no long-term debt. What it does owe are ”accounts payable” — the equivalent of a credit-card bill (usually paid within a short timeframe).</p>
<p>Now you can examine a company and see what it’s worth (on paper) and where the value lies. Google has no “inventory” (ever bought an off-the-shelf product from them?) but has a lot of cash, investments, and equipment. There’s very little debt and other liabilities, so it seems like a very stable company on paper; they won’t be going bankrupt anytime soon (there’s other documents that show how profitable the company is).</p>
<p><a href="http://finance.yahoo.com/q/bs?s=BBI">Blockbuster</a>, for example, has 2.5B in assets but 1.9B is owed to others (<a href="http://betterexplained.com/wp-content/uploads/accounting/bbi.htm">saved balance sheet here</a>). Shareholders aren’t left with much. In fact, it has 700M in “intangible assets”, so it actually has a <strong>negative</strong> amount of real, tangible assets. Not a good sign — if you liquidated the company today, it couldn’t pay off its debt.</p>
<h2>The Rules of the Game</h2>
<p>Accounting has many rules, but a basic one is this: use <strong>double-entry bookkeeping</strong>.</p>
<p>This fancy term means that <strong>all changes happen in pairs:</strong></p>
<ul>
<li>If assets go down, liabilities or owner’s equity should decrease also 
<li>If assets go up, liabilities or owner’s equity must increase as well </li></li></ul>
<p>Every change to assets must have a corresponding change to keep the equation in balance. There’s a formal system of “debits and credits” that describes these changes, but the concept is simple: if you make a change to one side, you must make one on the other as well.</p>
<h2>There’s More to Learn</h2>
<p>There’s much more to accounting, but you’ve got an idea of the basics:</p>
<ul>
<li>If a company has something, <strong>someone had better own it</strong> 
<li>A balance sheet <strong>lists assets, liabilities and owner’s equity</strong> at a point in time; everything must add up 
<li><strong>Changes must be made in pairs</strong>: if assets, liabilities or owner’s equity changes, something else much change as well </li></li></li></ul>
<p>Any system can be interesting (even “fun”) if you look at the reasons it was created and the problem it’s trying to solve. Could <em>you</em> have made a simpler way to report what a company is worth and who is owed what?</p>
<p>Enjoy.</p> <img src="http://feeds.feedburner.com/~r/Betterexplained/~4/188134911" height="1" width="1"></p></p></div>
</div>

<div class="feed">
  <div class="title">50. <a href="http://feeds.feedburner.com/~r/Betterexplained/~3/191186083/" target="_blank">How to Develop a Mindset for Math</a></div>
  <div class="content"><p>Math uses <strong>made-up rules</strong> to <strong>create models </strong>and<strong> derive relationships. </strong>When learning, I ask:</p> <ul> <li>What <strong>relationship</strong> does this model represent?  <li>What real-world items <strong>share this relationship</strong>?  <li>Does that relationship <strong>make sense to me</strong>?</li></li></li></ul> <p>It’s simple, but it helps me understand. If you liked my <a href="http://betterexplained.com/articles/category/math/">math posts</a>, this article covers my approach to this oft-maligned subject.</p> <h2>Math Education</h2> <p>Textbooks <strong>rarely</strong> focus on understanding; it’s mostly solving problems with “plug and chug” formulas. It saddens me that beautiful ideas get such a rote treatment:</p> <ul> <li><strong>The <a href="http://betterexplained.com/articles/surprising-uses-of-the-pythagorean-theorem/">Pythagorean Theorem</a> is not just about triangles</strong>. It is about the relationship between similar shapes, the distance between any set of numbers, and much more.  <li><strong>E is <a href="http://betterexplained.com/articles/an-intuitive-guide-to-exponential-functions-e/">not just a number</a></strong>. It is about the fundamental relationships between all growth rates.  <li><strong>The <a href="http://betterexplained.com/articles/demystifying-the-natural-logarithm-ln/">natural log</a> is not just an inverse function</strong>. It is about the amount of time things need to grow.</li></li></li></ul> <p>Elegant, “a ha!” insights should be our focus, but we leave that for students to randomly stumble upon themselves. I hit an “a ha” moment after a hellish cram session in college; since then, I’ve wanted to find and share those epiphanies to spare others the same pain.</p> <p>But it’s a selfish goal too — I want to convince you to share your insights with me, too. There’s more understanding, less pain, and everyone wins.</p> <h2>Math Evolves Over Time</h2> <p>I consider math as a way of thinking, and it’s important to see <strong>how</strong> that thinking developed rather than only showing the result. Let’s try an example.</p> <p>Imagine you’re a caveman doing math. One of the first problems will be <strong>how to count things</strong>. Several systems have developed over time:</p> <p><img src="http://betterexplained.com/wp-content/uploads/understanding/number-systems.png"> </p> <p>No system is “right”, and each has advantages:</p> <ul> <li><strong>Unary system: </strong>Draw lines in the sand — as simple as it gets. Great for keeping score in games; you can add to a number without erasing and rewriting.  <li><strong>Roman Numerals:</strong> More advanced unary, with shortcuts for large numbers.  <li><strong>Decimals</strong>: Huge realization that numbers can use a “positional” system with place and zero.  <li><strong>Binary:</strong> Simplest positional system (two digits, on vs off) so it’s great for mechanical devices.  <li><strong>Scientific Notation: </strong>Extremely compact, can easily gauge a number’s size and precision (1e3 vs 1.000e3).</li></li></li></li></li></ul> <p>Think we’re done? No way. In 1000 years we’ll have a system that makes decimal numbers look as quaint as Roman Numerals (<em>“By George, how did they manage with such clumsy tools?”</em>).</p> <h2>Negative Numbers Aren’t That Real</h2> <p>Let’s think about numbers a bit more. The example above shows <strong>our number system is one of many ways to solve the “counting” problem.</strong> </p> <p>The Romans would consider zero and fractions “strange”, but it doesn’t mean “nothingness” and “part to whole” aren’t useful concepts. But see how each system incorporated new ideas.</p> <p>Fractions (1/3), decimals (.234), and complex numbers (3 + 4i) are ways to <strong>express new relationships</strong>. They may not “make sense” right now, just like zero didn’t “make sense” to the Romans. We need new real-world relationships (like debt) to make them make sense.</p> <p>Even then, negative numbers may not exist in the way we think, as you convince me here:</p> <blockquote> <p><strong>You: </strong>Negative numbers are a great idea, but don’t inherently exist. It’s a label we apply to a concept.<br><strong>Me:</strong> Sure they do.<br><strong>You:</strong> Ok, show me -3 cows. <br><strong>Me: </strong>Well, um… assume you’re a farmer, and you lost 3 cows.<br><strong>You:</strong> Ok, you have zero cows.<br><strong>Me: </strong>No, I mean, you gave 3 cows to a friend.<br><strong>You:</strong> Ok, he has 3 cows and you have zero.<br><strong>Me: </strong>No, I mean, he’s going to give them back someday. He owes you.<br><strong>You: </strong>Ah. So -3 means “somebody owes me?” and forces them to repay you? That’s pretty neat how a number can change behavior – I should use that trick on the kid who borrowed my xbox.<br><strong>Me: </strong>Sigh. It’s not like that. When he gives you the cows back, you go from -3 to 3.<br><strong>You: </strong>Cool, he gives you 3 cows and you jump 6, from -3 to 3? Amazing arithmetic you’ve got there. Care to show me <em>sqrt(-17)</em> cows?<br><strong>Me: </strong>Get out.</p></blockquote> <p>Negative numbers can <strong>express a relationship:</strong></p> <ul> <li><strong>Positive numbers</strong> represent a surplus of cows  <li>Zero represents no cows  <li><strong>Negative numbers </strong>represent a deficit of cows that are assumed to be paid back</li></li></li></ul> <p>But the negative number “isn’t really there” — there’s only the <strong>relationship they represent </strong>(a surplus/deficit of cows). We’ve created a “negative number” model to help with bookkeeping, even though you can’t hold -3 cows in your hand. (I purposefully used a non-standard interpretation of what “negative” means; it’s a different counting system, just like Roman numerals and decimals)</p> <p>By the way, negative numbers <a href="http://en.wikipedia.org/wiki/Negative_and_non-negative_numbers#First_usage_of_negative_numbers">weren’t accepted</a> by many people, including Western mathematicians, until the 1700s. The idea of a negative was considered “absurd”. Negative numbers <strong>do</strong> seem strange unless you can see how they represent complex real-world relationships, like debt.</p> <h2>Why All the Philosophy?</h2> <p>I realized that my <strong>mindset is key to learning. </strong>It helped me arrive at deep insights, specifically:</p> <ul> <li><strong>Factual knowledge is not understanding.</strong> Knowing “hammers drive nails” is not the same as the insight that any hard object (a rock, a wrench) can drive a nail.  <li><strong>Keep an open mind. </strong>Develop your intuition by allowing yourself to be a beginner again.</li></li></ul> <blockquote> <p>A university professor went to visit a famous Zen master. While the master quietly served tea, the professor talked about Zen. The master poured the visitor’s cup to the brim, and then kept pouring. The professor watched the overflowing cup until he could no longer restrain himself. “It’s overfull! No more will go in!” the professor blurted. “You are like this cup,” the master replied, “How can I show you Zen unless you first empty your cup.”</p></blockquote> <ul> <li><strong>Be creative. </strong>Look for strange relationships. Use diagrams. Use humor. Use analogies. Use mnemonics. Use anything that makes the ideas more vivid. Analogies aren’t perfect but help when struggling with the general idea.  <li><strong>Realize you can learn. </strong>We expect kids to learn algebra, trigonometry and calculus that would astound the ancient Greeks. And we should: we’re capable of learning so much, if explained correctly. Don’t give up until it makes sense or that mathematical gap will haunt you. Mental toughness is critical — we often give up too easily.</li></li></ul> <h2>So What’s the Point?</h2> <p>I want to share what I’ve discovered, hoping it helps you learn math:</p> <ul> <li>Math creates <strong>models</strong> that have certain <strong>relationships</strong>  <li>We try to find <strong>real-world phenomena</strong> that have the same relationship  <li>Our models are <strong>always improving</strong>. A new model may come along that better explains that relationship (roman numerals to decimal system).</li></li></li></ul> <p>Sure, some models <em>appear</em> to have no use: <strong>“What good are imaginary numbers?”</strong>, many students ask. It’s a valid question.  <p>The use of imaginary numbers is limited by our imagination and understanding — just like negative numbers are “useless” unless you have the idea of “debt”, imaginary numbers don’t make “sense” because we don’t understand their relationship well enough.  <p><strong>Math provides models; understand their relationships and apply them to real-world objects.</strong>  <p>Developing intuition makes learning fun –  even <a href="http://betterexplained.com/articles/understand-accounting-basics-aloe-and-balance-sheets/">accounting</a> isn’t bad when you understand the problems it solves. I want to cover cover complex numbers, calculus and other elusive topics by focusing on relationships, not proofs and mechanics.  <p>But this is my experience — how do you learn best?</p> <img src="http://feeds.feedburner.com/~r/Betterexplained/~4/191186083" height="1" width="1"></p></p></p></p></div>
</div>

<div class="feed">
  <div class="title">51. <a href="http://feeds.feedburner.com/~r/Betterexplained/~3/195389530/" target="_blank">Slogan Contest: Help describe this site!</a></div>
  <div class="content"><p> <strong>I’m looking for a site tagline, and would love your help</strong>. Just something to explain the site theme in a few words, such as:</p>


<ul>
<li>New York Times: All the news that’s fit to print.</li>
<li>Mad Magazine: All the news that fits, we print.</li>
</ul>



<p>I’d like to say this site’s tone is somewhere in between <img src="http://betterexplained.com/wp-includes/images/smilies/icon_smile.gif" alt=":)"> . The goal is to emphasize understanding over memorization, the sharing of “a ha” insights, and the idea that anything can be explained simply. Here’s a few of my attempts:</p>


<ul>
<li>Know how? Know why.</li>
<li>My learning pain. Your learning gain.</li>
<li>As simple as possible, but no simpler. (Einstein quote).</li>
</ul>



<p>But I know you can do better — enter a fun, bold slogan here:</p>



<p>You can enter as much as you like, for a chance at:</p>


<ul>
<li>First place: $50 Amazon gift certificate</li>
<li>Second place: $25 Amazon gift certificate</li>
<li>Third place:$25 Amazon gift certificate</li>
</ul>



<p>This contest ends 5:00 PM <span>EST </span>on 12/14/07. I’ll pick the top 3 entries and post the results. Have fun — I want to give out some gifts for the holidays!</p>

<p><span>PS.</span> I’m always up to projects. My friends and I would appreciate feedback on our idea above: making promotions fun and easy for site owners. </p><img src="http://feeds.feedburner.com/~r/Betterexplained/~4/195389530" height="1" width="1"></div>
</div>

<div class="feed">
  <div class="title">52. <a href="http://feeds.feedburner.com/~r/Betterexplained/~3/196794303/" target="_blank">A Quirky Introduction To Number Systems</a></div>
  <div class="content"><p>Everyone’s got quirks. Me, I like finding new ways to think about problems, and I’ve started seeing numbers in a new way. Today I’m bringing you along for the ride.</p> <p>And why do you care?</p> <ul> <li>You’ll see math as <strong>one of several approaches</strong>. We’re still improving our number system.</li> <li>You’ll start noticing <strong>core relationships</strong> and apply them to diverse areas</li> <li>You’ll have a way to approach “weird” concepts like division by zero and <strong>imaginary numbers</strong>.</li></ul> <h2>Math is Software For Your Brain</h2> <h2></h2> <h2></h2> <p>Though our favorite encyclopedia describes math as “the body of knowledge centered on such concepts as quantity, structure, space, and change” I think there’s more to it than that.</p> <p><strong>Math is software for your brain:</strong></p> <ul> <li>Your brain is a raw computer.</li> <li>You learn new math (install “Counting 1.0″ and “Algebra XY Pro”) and suddenly you can solve new types of problems.</li> <li><strong>Sometimes math has bugs.</strong> “Roman Numerals I” was ok, but <strong>Decimals 2.0</strong> was a much-needed upgrade. But we still have a few issues, like dividing by zero. </li></ul> <p>It’s a strange analogy, but I’m a bit strange, so I think it works out.</p> <h2>But Math Has Its Limits</h2> <p>Quick quiz: Can you multiply two Roman numerals? No cheating, no converting to decimal: I’m talking about “IX times <span>XXXIV</span>“. Ready, set, multiply!</p> <p>…</p> <p>Having fun yet? It’s <span>CCCVI.</span></p> <p>Does this horrendous experience mean multiplication is “hard”? Or are we thinking about multiplication in the wrong way, <strong>using the wrong mental software</strong>?</p> <p>If you upgrade your brain from “Roman Numerals I” to “Decimals 2.0″, you’ll find that 9 times 34 is a much easier question: after some work you’d get 306. Same problem + different mental software = drastic difference.</p> <h2>Painting With Notepad</h2> <p>Yes, you could squeeze multiplication into Roman Numerals. But it’s neither fun nor easy, and don’t get me started on long division.</p> <p><strong>Our difficulties are often due to our approach, not the concept.</strong></p> <p>Think of it like trying to draw in Notepad. It’s a nice tool, and you can even “type” the <a href="http://www.glassgiant.com/ascii/ascii.php?sample=mona">Mona Lisa</a>, but the software just wasn’t built with images in mind.</p> <p><img src="http://betterexplained.com/wp-content/uploads/numbers/mona_side_by_side.png"> </p> <p>Similarly, Roman Numerals were built <strong>when we were still learning to count</strong> — zero wasn’t even invented yet! Math is a software system that gets better over time, and <strong>Roman Numerals were due for an upgrade</strong>.</p> <p>But before we get too high-and-mighty, realize our <strong>current number system</strong> is a patchwork of new features and bug fixes, used to improve our understanding of the universe.</p> <p>And when we hit difficulties (What’s 1/0? The square root of -1?) we need to wonder if we’re hitting universal “laws” or walls of our own making. Like the Romans trying to multiply, let alone do fractions, my money’s on the latter.</p> <h2>From Ug to Infinity</h2> <p>Our number system <a href="http://betterexplained.com/articles/how-to-develop-a-mindset-for-math/">developed over time</a>. We started counting on our fingers, moved to unary (lines in the sand), Roman Numerals (shortcuts for large numbers) and Arabic Numerals (the decimal system) with the invention of zero.</p> <p>Along the way we found “bugs” in our number system and had to invent new ways around it. Again, <strong>the bug was in our thinking (our mental software). </strong></p> <p><strong>Ugware</strong></p> <p>Ugware is the counting system devised by Ug the caveman: counting on your fingers and toes. Ug’s bug was that he was limited to 20 items!</p> <p>The fix was to abstract the need for physical objects: you don’t need 20 cows to count 20 cows. You can make 20 lines in the sand. Or take shortcuts like C for 100.</p> <p><strong>Unary and Roman Numerals</strong></p> <p>Having numbers represented abstractly let us do cool things like add and subtract, even fairly large numbers. I + II = <span>III.</span> X + XX = <span>XXX.</span> Not bad.</p> <p>But there was still a few “bugs” — what is <span>III </span>- <span>III</span>?</p> <p><strong>Zero</strong></p> <p>What a fantastic, beautiful invention: using the symbol 0 to represent nothingness! It’s a mind-bending and useful idea: we can keep track of “no” cows at all!</p> <p>This development led to our familiar positional number system: 204 means two “hundreds”, zero “tens” and four ones.</p> <p><strong>Integer division and multiplication became possible</strong> in ways the Romans (and Ug) had never imagined. You could work out 1234 × 5678 if given enough time. What a great feature!</p> <p><strong>Negatives</strong></p> <p>But zero didn’t solve everything; subtraction still had problems. What happens when we take 5 from 3? One solution is to throw up our hands and say “it’s a bug and it’s undefined”, but we’ll do better.</p> <p>We can think about the problem differently, and entertain the possibility that a number can be “negative” — a number that is less than nothing! (Pretty mind-bending, no?). </p> <p>There are many interpretations (a lack of cows, a debt of cows) and negatives were invented to handle this “bug” in subtraction. Of course, it took a few thousand years to accept this new feature — negative numbers were still controversial in the 1700s!</p> <p><strong>Rational Numbers</strong></p> <p>Division introduced bugs as well. 8/4 is fine, but what is 3/4? It’s a bug!</p> <p>The fix is to find a way to represent “numbers between numbers”. 3/4 is really 75/100, or “0.75″.</p> <p>We invented the decimal point to handle the <strong>crazy idea of a number more than zero but less than one</strong>. Wow! Pretty wild, but we included these crazy types of numbers to make our mental software better. Lo and behold, fractions have their uses. The average family can have 2.3 kids and we know what it means.</p> <p><strong>Irrationals Make Greeks Angry</strong></p> <p>Here we are, minding our own business when we see a right triangle:</p> <p>The sides are 1 and 1. And there, staring us square in the face, is the square root of 2. It taunts us, asking to be written down. We can’t — it’s an infinite, non-repeating decimal number that can’t be expressed as a fraction! And yet it’s right there on paper.</p> <p>It’s more than a conundrum — it’s madness! The guy who discovered irrationals got <a href="http://en.wikipedia.org/wiki/Hippasus">thrown off a boat</a>.</p> <p>Luckily, irrationals are at least “algebraic” in that they are the solution to some algebra equation. We can consider sqrt(2) as shorthand for “the solution to the equation x^2 = 2″. We often forget sqrt(9) is really both 3 and -3, don’t we? Convention implies the positive root.</p> <p><strong>Complex Numbers</strong></p> <p>Now some a smart aleck asks, “Ok bub, what number is the solution to the equation x^2 = -1?”.</p> <p>What to do? Declare this to be <strong>impossible and non-sensical</strong>, just like zero, fractions, rationals and irrationals were once “impossible and non-sensical”? Or do we accept that maybe, just maybe, our human understanding of the universe is not complete and we have more to learn. You know where my money lies.</p> <p><strong>Imaginary numbers are just as “realistic” as other numbers</strong> (or equally contrived, depending on your viewpoint). But, we don’t have an intuition for them because they’re often “explained”: <em>Oh, you don’t have an Electrical Engineering degree? Didn’t learn about complex impedance? No intuitive imaginary numbers for you!</em></p> <p>I’ve been thinking about these numbers and plan to address this issue. But not yet — have patience.</p> <h2>Why .9999… = 1, and why you should care</h2> <p>Our number system is a way of thinking, but it still has a few gaps. We’re not quite sure how to deal with infinity and infinitely small numbers.</p> <p>Here’s a brain-buster for you:</p> <blockquote> <p><strong>You:</strong> What’s 1/3?<br><strong>Me: </strong>Um, .33 repeating.<br><strong>You: </strong>Ok. What’s 1/3 + 1/3?<br><strong>Me: </strong>2/3<br><strong>You: </strong>Sure, but do it in decimals.<br><strong>Me: </strong>Um, .33 repeating plus .33 would be… .666 repeating.<br><strong>You: </strong>Great. Now what’s 1/3 + 1/3 + 1/3, in decimal?<br><strong>Me: </strong>Uh… .33 repeating plus .33 repeating plus .33 repeating… is .99 repeating.<br><strong>You: </strong>But doesn’t 1/3 + 1/3 + 1/3 = 1? <strong>So .99 repeating = 1.<br>Me:</strong> What manner of trickery is this? (Pushes you off blog).</p></blockquote> <p>Try that argument on a kid (or adult) — it’s fun to see people’s reactions. Clearly, 1/3 + 1/3 + 1/3 = 1, but somehow when we try to “add it in decimal” the result seems a bit strange. <strong>Again, is the strangeness due to the concept, or our thinking?</strong></p> <p>What is .33 repeating, anyway? Is it a monkey writing 3’s until the end of time? Is it a number <strong>beyond our notation</strong> that we’re hopelessly trying to approximate, like the square root of 2? If we simply switch to base 3, the problem goes away: 1/3 = .1 in base 3, so .1 + .1 + .1 = 1 (again, in base 3).<br><br>And why do you care? <strong>It may be time for a number system upgrade</strong>. Discussing infinity with our current numbers is like drawing in notepad. It’s crude and feels “tacked on” (like saying 1/0 = infinity. What about 2/0 or 0/0?).</p> <p>Mathematicians are working on <a href="http://en.wikipedia.org/wiki/Extended_real_number_line">new number systems</a> where infinity is built-in, but there’s still unsolved problems about how to “count” infinity. </p> <p>Let’s seek the “a ha!” insights that made zero, fractions and negative numbers understandable, not just the results of manipulating equations. We’ve been able to overcome every previous mathematical roadblock.</p> <h2>Going Forward</h2> <p>The goals of this article were simple:</p> <ul> <li>Show how math is like <strong>mental software</strong> that <strong>improves over time</strong></li> <li>Explain that “nonsense” like zero or negative numbers can <strong>start as a paradox and become intuitive</strong> as we adapt our approach.</li> <li>Today, <strong>we still have trouble</strong> with ideas like infinity (or at least I do). It’s ok to admit it.</li></ul> <p>This is a way to think about math; combine it with your own understanding. Don’t stand in a daze, unable to add because you’re unsure what 1/3 really means.</p> <p>Insights deepen our understanding, but sometimes only emerge with use. Newton didn’t have a “formal” understanding of infinitesimals when he invented calculus, but it seemed to work fine for him (equations got solved). I don’t advocate plug-and-chug, but for certain ideas you need to hammer away before the insights come.</p> <p>But enough philosophy. Upcoming articles will show real, concrete ways to think about arithmetic and complex numbers, which can aid the “mechanical” understanding we have today. Happy math.</p> <p><span>PS.</span> If you’re curious, there’s more on <a href="http://en.wikipedia.org/wiki/0.999...">.999…</a> and <a href="http://en.wikipedia.org/wiki/Division_by_zero">division by zero</a> on wikipedia.</p> <img src="http://feeds.feedburner.com/~r/Betterexplained/~4/196794303" height="1" width="1"></div>
</div>

<div class="feed">
  <div class="title">53. <a href="http://feeds.feedburner.com/~r/Betterexplained/~3/200043281/" target="_blank">Rethinking Arithmetic: A Visual Guide</a></div>
  <div class="content"><p> Arithmetic gives us tools to <strong>smoosh, slide and stretch</strong> numbers. These transformations are handy: sometimes there’s things in the real world we want to <strong>smoosh, slide and stretch</strong> in the same way.</p>

<p>Why’s this important?</p>

<p>Seeing arithmetic as a <strong>type of transformation</strong> prepares you to make sense of seemingly-weird concepts, like the square root of -1, and visualize problems in a new way. Let’s take a look.</p>

<h2>Addition</h2>

<p>Addition is simple, yet it can mean several things:</p>


<ul>
<li><strong>Accumulate</strong>: Count up similar quantities (often for tangible items).</li>
<li><strong>Slide</strong>: Shifting a number along a scale (for less tangible things, like temperature).</li>
<li><strong>Combine</strong>: Make a new quantity out of two different ones (like notes in a music chord).</li>
</ul>



<p><img src="http://betterexplained.com/wp-content/uploads/arithmetic/addition.png" alt="Addition viewed as accumulate, slide or combine"></p>

<p>What’s the right meaning? It depends on the context. When adding apples, we count up similar items (3 apples + 4 apples = 7 apples). When measuring temperature, we add heat to move along a scale (3 degrees + 4 degrees = 7 degrees). </p>

<p>When adding vectors, a combination makes the most sense: 3 blocks east + 4 blocks north = 5 blocks in a new direction (”as the crow flies” distance). In this case, you must track the component parts, keeping “North” and “East” separate. When adding apples, you can combine everything: once you have 7, you don’t care that it was once 3 and 4.</p>

<p>A single operation (addition) can take on several intuitive meanings. This list isn’t exhaustive — they are the interactions I’ve noticed, and I’m sure you have others.</p>

<h2>Multiplication</h2>

<p>Multiplication can also be interpreted in several ways:</p>


<ul>
<li><strong>Repetition</strong>: Performing multiple additions.</li>
<li><strong>Scaling</strong>: Making a number grow or shrink all at once.</li>
</ul>



<p><img src="http://betterexplained.com/wp-content/uploads/arithmetic/multiplication.png" alt="Multiplication viewed as repetition or scaling"></p>

<p>Context determines our meaning. With apples, “4x” means turning an order of 2 apples into an order of 8 (4 groups of 2). With photo software, “4x” means expanding a 2-inch photo to be 8 inches long. Each meaning is different: you’d be annoyed if I gave you a giant, 5-lb apple or 4 separate photos.</p>

<p>In a narrow sense, multiplication is “repeated addition”. Sure. But that’s not always the easiest interpretation — care to “repeatedly add” 7.3 times?</p>

<h2>Negatives and Inverses</h2>

<p>Negatives and inverses both represent the idea of “reverse” or “opposite”. But that’s ambiguous: What’s the opposite of multiplying by two?</p>

<p><img src="http://betterexplained.com/wp-content/uploads/arithmetic/negative_inverse.png" alt="Negative and inverse multiplication"></p>

<p>“Opposite” can mean a few things:</p>


<ul>
<li><strong>Multiply by 1/2</strong>: Turn a profit of 1 into a profit of 1/2 (”unscale” it)</li>
<li><strong>Multiply by -2</strong>: Turn a profit of 1 into a loss of 2 (flip it)</li>
</ul>




<p>Yet again, our context determines meaning. When a company “reverses a gain” it implies a loss, aka multiplying by a negative. When we “reverse a zoom” in photo software, we want to shrink the photo (not mirror-image it), so we multiply by 1/2. Context, context, context (tired of that word yet?).</p>

<p>When adding, there’s only one type of opposite: the reverse of +8 is -8. But the trick is to know that -8 really means “0-8″: you’re moving backwards relative to some reference point. Moving “in reverse” means different things depending if your original direction was East or North.</p>

<h2>What’s in an equation?</h2>

<p>Equations ask questions. When you see </p>

<p><img src="http://betterexplained.com/latexrender/pictures/a9689703566da06418cfde37ae65ff48.gif" title="\displaystyle{x^2 = 9}" alt="\displaystyle{x^2 = 9}"></p>

<p>It’s more than just a plug and chug problem. Think about the question like this:</p>

<p><img src="http://betterexplained.com/latexrender/pictures/6574870fe4324c77dccfc933545f5863.gif" title="\displaystyle{1 * x^2 = 9}" alt="\displaystyle{1 * x^2 = 9}"></p>

<p>What <strong>transformation</strong> (”times x”), when applied twice, will turn 1 into 9? </p>

<p><img src="http://betterexplained.com/wp-content/uploads/arithmetic/square_root.png" alt="Visual diagram of squares, 3 and -3"></p>

<p>We have two answers:</p>


<ul>
<li><strong>Scale by 3 (times 3)</strong>: Do it twice and you’ll get 9: 1 * 3 * 3 = 9</li>
<li><strong>Scale by 3 and flip (times -3)</strong>: Done twice, you get 9 also: 1 * -3 * -3 = 9</li>
</ul>



<p>Nifty. I included “1″ to show <strong>what</strong> is being transformed. Sure, it’s optional, but it’s not something we think about. What is the “times 3″ acting on?</p>

<p>Stepping back this way, we can see arithmetic as a method to push, pull, tug and squeeze one number into another. We’ve managed to turn one large transformation (”times 9″) into two equal, smaller ones (”times -3″ or “times 3″).</p>

<h2>Real-World Example: Random Numbers</h2>

<p>Enough theory — let’s show this mindset in action. Most programming languages offer a random() function that gives a number from 0 to 1. But what if you want something from 5-10?</p>

<p>The question is: how do I <strong>transform</strong> my range of 0-1 into a range from 5-10?</p>

<p><img src="http://betterexplained.com/wp-content/uploads/arithmetic/random_numbers.png" alt="Diagram of scaling random numbers to new ranges"></p>

<p>Arithmetic to the rescue!</p>


<ul>
<li>First, you <strong>stretch</strong> 0-1 into 0-5 by multiplying by 5</li>
<li>Next, <strong>slide</strong> 0-5 to 5-10 by adding 5</li>
<li>And tada. You have a range from 5-10. </li>
</ul>



<p>Try it out below. You start with a number “r” and transform it into the proper range.</p>

<p></p>

<p>By the way, this range could be the ages 18-65, the years 1960-2007, or the temperatures 30F - 80F for use in your simulation (everyone runs simulations, right?). No matter your range, you can start with the “0 to 1″ building block and modify it.</p>

<p>Sure, you can figure this out without a diagram, but sometimes it’s nice to <strong>visualize</strong> what’s happening. Our brain is a vision-processing supercomputer, so let’s use its strengths.</p>

<h2>What’s next?</h2>

<p>This post introduces the idea that <strong>arithmetic is a transformation</strong>. You bend numbers into other ones, and each transformation has a meaning. Some fit a situation better than others: use the one you like most.</p>

<p>The goal isn’t to turn multiplication into a cumbersome diagramming process. It’s a technique, <a href="http://betterexplained.com/articles/how-to-develop-a-mindset-for-math/">a mindset</a>, a new weapon to use against seemingly complex operations. </p>

<p>When studying linear algebra (matrices), you can view multiplication as a type of transformation (scaling, rotating, skewing), instead of a bunch of operations that change a matrix around. This approach will help when <a href="http://betterexplained.com/articles/a-visual-intuitive-guide-to-imaginary-numbers/">we cover imaginary numbers</a>, that foul beast which has befuddled many students.</p>

<p>Little insights help bigger ideas click. Happy math.<br>
 </p><img src="http://feeds.feedburner.com/~r/Betterexplained/~4/200043281" height="1" width="1"></div>
</div>

<div class="feed">
  <div class="title">54. <a href="http://feeds.feedburner.com/~r/Betterexplained/~3/202518842/" target="_blank">Slogan Contest: The Winners Are In</a></div>
  <div class="content"><p> Thanks for your slogans! There were nearly 200, and I had a tough time deciding, but here are the winning entries:</p>


<ul>
<li><em>James</em>: Learn right, not rote (1st place: $50 Amazon Gift Certificate)</li>
<li><em>Matt</em>: Because ignorance is overrated (2nd place: $25 Amazon Gift Certificate)</li>
<li><em>Mark</em>: The stuck stops here (3rd place: $25 Amazon Gift Certificate)</li>
</ul>



<p>Congratulations guys! There were numerous honorable mentions — I was impressed by how you conveyed ideas in so few words:</p>

<p><strong>Humor</strong></p>


<ul>
<li><em>slashdotter</em>: Xor <span>HUH </span>to <span>HAH. </span>(XOR is a programmer’s way to selectively “toggle” data; you can imagine transforming just the U to an A. It’s a nice bit of geek humor).</li>
</ul>



<p><strong>Inspiring Quotes</strong></p>


<ul>
<li><em>lkurts</em>: Pauca sed matura (few but ripe) — Carl Friedrich Gauss. (I like the quote, and  it helps remind me to <a href="http://betterexplained.com/articles/build-a-site-you-and-your-readers-will-love/">focus on quality</a>, not quantity)</li>
<li><em>nandan</em>: Nothing in life is to be feared. It is only to be understood. — Marie Curie. (I agree — many of our <a href="http://betterexplained.com/articles/how-to-develop-a-mindset-for-math/">limitations in learning</a> are due to fear).</li>
</ul>



<p><strong>Nice Rhymes</strong> All of these entries had a nice ring to them.</p>


<ul>
<li><em>Joel</em>: From complexity and cacophony to simplicity and epiphany.</li>
<li><em>Mark</em>: “The Explanation Station” and “BetterExplained means better retained”.</li>
<li><em>Jeff</em>: Confusion diffusion.</li>
</ul>



<p><strong>Capturing The Philosophy</strong> Interesting thoughts on that “a ha” feeling.</p>


<ul>
<li><em>Cheeaun</em>: Learning hurts. Explanations don’t.</li>
<li><em>Patrick</em>: Insight, in hindsight.</li>
<li><em>Labeaux</em>: …and the lightbulb in my brain turned on!</li>
<li><em>Joel</em>, <em>Nasir</em>: “Explanations for human beings” and “Explanations for the rest of us”</li>
<li><em>Brad</em>: Have you had your “a ha” today?</li>
</ul>



<p>Thanks to everyone who participated (browse entries on the <a href="http://betterexplained.com/articles/slogan-contest-help-describe-this-site/">contest page</a>). It helped me learn what elements of the site you enjoy, and what themes to focus on. Stay tuned for the next article, on complex numbers. </p><img src="http://feeds.feedburner.com/~r/Betterexplained/~4/202518842" height="1" width="1"></div>
</div>

<div class="feed">
  <div class="title">55. <a href="http://feeds.feedburner.com/~r/Betterexplained/~3/203914123/" target="_blank">A Visual, Intuitive Guide to Imaginary Numbers</a></div>
  <div class="content"><p> Imaginary numbers always confused me. Like <a href="http://betterexplained.com/articles/an-intuitive-guide-to-exponential-functions-e/">understanding e</a>, most explanations fell into one of two categories:</p>


<ul>
<li>It’s a mathematical abstraction, and the equations work out. <strong>Deal with it.</strong></li>
<li>It’s used in advanced physics, trust us. <strong>Just wait until college.</strong></li>
</ul>



<p>Gee, what a great way to encourage math in kids! Today we’ll assault this topic with our favorite tools:</p>


<ul>
<li><a href="http://betterexplained.com/articles/how-to-develop-a-mindset-for-math/"><strong>Focusing on relationships</strong></a>, not mechanical formulas.</li>
<li><a href="http://betterexplained.com/articles/a-quirky-introduction-to-number-systems/"><strong>Seeing complex numbers as an upgrade to our number system</strong></a>, just like zero, decimals and negatives were.</li>
<li><a href="http://betterexplained.com/articles/rethinking-arithmetic-a-visual-guide/"><strong>Using visual diagrams</strong></a>, not just text, to understand the idea.</li>
</ul>



<p>And our secret weapon: <strong>learning by analogy.</strong> We’ll approach imaginary numbers by observing its ancestor, the negatives. Here’s your guidebook:</p>

<p><img src="http://betterexplained.com/wp-content/uploads/complex/negative_imaginary_table.png" alt="imaginary number properties"></p>

<p>It doesn’t make sense yet, but hang in there. By the end we’ll hunt down <em>i</em> and put it in a headlock, instead of the reverse.</p>

<h2>Really Understanding Negative Numbers</h2>

<p>Negative numbers aren’t easy. Imagine you’re a European mathematician in the 1700s. You have 3 and 4, and know you can write 4 - 3 = 1. Simple.</p>

<p>But what about 3-4? What, exactly, does that mean? How can you take 4 cows from 3? <em>How could you have less than nothing?</em></p>

<p>Negatives were considered <strong>absurd</strong>, something that “darkened the very whole doctrines of the equations” (<a href="http://en.wikipedia.org/wiki/Negative_and_non-negative_numbers#First_usage_of_negative_numbers">Francis Maseres, 1759</a>). Yet today, it’d be <strong>absurd</strong> to think negatives aren’t logical or useful. Try asking your teacher whether negatives corrupt the very foundations of math.</p>

<p>What happened? We invented a <em>theoretical number that had useful properties</em>. Negatives aren’t something we can touch or hold, but they describe certain relationships well (like debt). <strong>It was a useful fiction</strong>.</p>

<p>Rather than saying “I owe you 30″ and reading words to see if I’m up or down, I can write “-30″ and know it means I’m in the hole. If I earn money and pay my debts (-30 + 100 = 70), I can record the transaction easily. I have +70 afterwards, which means I’m in the clear.</p>

<p>The positive and negative signs <strong>automatically keep track of the direction</strong> — you don’t need a sentence to describe the impact of each transaction. Math became easier, more elegant. It didn’t matter if negatives were “tangible” — they had useful properties, and we used them until they became everyday items. Today you’d call someone obscene names if they didn’t “get” negatives.</p>

<p>But let’s not be smug about the struggle: negative numbers were a huge mental shift. Even Euler, the genius who discovered e and much more, didn’t understand negatives as we do today. They were considered “meaningless” results (he made up for this in style). It’s a testament to our mental potential that a modern child can understand ideas that confounded ancient mathematicians.</p>

<h2>Enter Imaginary Numbers</h2>

<p>Imaginary numbers have a similar story. We can solve equations like this all day long:</p>

<p><img src="http://betterexplained.com/latexrender/pictures/a9689703566da06418cfde37ae65ff48.gif" title="\displaystyle{x^2 = 9}" alt="\displaystyle{x^2 = 9}"></p>

<p>The answers are 3 and -3. But suppose some wiseguy puts in a teensy, tiny minus sign:</p>

<p><img src="http://betterexplained.com/latexrender/pictures/a339521a3589c0a0077c2d9000943b4d.gif" title="\displaystyle{x^2 = -9}" alt="\displaystyle{x^2 = -9}"></p>

<p>Uh oh. This question makes most people cringe the first time they see it. <em>You want the square root of a number less than zero? That’s absurd!</em></p>

<p>It seems crazy, just like negatives, zero, and irrationals (non-repeating numbers) must have seemed crazy at first. There’s no “real” meaning to this question, right?</p>

<p>Wrong. So-called “imaginary numbers” are as normal as every other number (or just as fake): they’re a tool to describe the world. In the same spirit of assuming -1, .3, and 0 “exist”, let’s assume some number <em>i</em> exists where:</p>

<p><img src="http://betterexplained.com/latexrender/pictures/7629fbf1f6929348712245765c35727a.gif" title="\displaystyle{i^2 = -1}" alt="\displaystyle{i^2 = -1}"></p>

<p>That is, you multiply <em>i</em> by itself to get -1. What happens now?</p>

<p>Well, first we get a headache. But playing the “Let’s pretend <em>i</em> exists” game actually makes math easier and more elegant. <strong>New relationships emerge</strong> that we can describe with ease.</p>

<p>You may not believe in <em>i</em>, just like those fuddy old mathematicians didn’t believe in -1. New, brain-twisting concepts are <strong>hard</strong> and they don’t make sense immediately, even for Euler.</p>

<p>But just because a concept is strange, doesn’t mean it’s not useful. I really don’t like the term “imaginary number” — it was considered an insult, a slur, designed to hurt <em>i</em>’s feelings because it was different. It’s just as real as everything else, but the name stuck, so we’ll use it.</p>

<h2>Visual Understanding of Negative and Complex Numbers</h2>

<p><a href="http://betterexplained.com/articles/rethinking-arithmetic-a-visual-guide/">As we saw last time</a>, the equation x^2 = 9 really means: </p>

<p><img src="http://betterexplained.com/latexrender/pictures/64b320ad6a60340af97b50f3732fed9c.gif" title="\displaystyle{1 \cdot x^2 = 9}" alt="\displaystyle{1 \cdot x^2 = 9}"></p>

<p><em>What transformation x, when applied twice, turns 1 to 9?</em></p>

<p>The two answers are “x = 3″ and “x = -3″: That is, you can “scale by” 3 or “scale by 3 and flip” (flipping or taking the opposite is one interpretation of multiplying by a negative).</p>

<p>Now let’s think about x^2 = -1, which is really</p>

<p><img src="http://betterexplained.com/latexrender/pictures/2ca9c7e2e2aa135e7588c1c4456e0bd5.gif" title="\displaystyle{1 \cdot x^2 = -1}" alt="\displaystyle{1 \cdot x^2 = -1}"></p>

<p><em>What transformation x, when applied twice, turns 1 into -1?</em> Hrm.</p>


<ul>
<li>We can’t multiply by a positive twice, because the result stays positive</li>
<li>We can’t multiply by a negative twice, because the result will flip back to positive on the second multiplication</li>
</ul>



<p>But what about… a <strong>rotation</strong>! It sounds crazy, but if we imagine x being a “rotation of 90 degrees”, then applying x twice will be a 180 degree rotation, or a flip from 1 to -1!</p>

<p><img src="http://betterexplained.com/wp-content/uploads/complex/imaginary_rotation.png" alt="Imaginary Number Rotation"></p>

<p>Yowza! And if we think about it more, we could rotate twice in the other direction (clockwise) to turn 1 into -1. This is “negative” rotation or a multiplication by -i:</p>

<p><img src="http://betterexplained.com/wp-content/uploads/complex/positive_negative_rotation.png" alt="Negative Rotation"></p>

<p>If we multiply by -i twice, we turn 1 into -i, and -i into -1. So there’s really <em>two</em> square roots of -1: <em>i</em> and <em>-i</em>.</p>

<p>This is pretty cool. We have some sort of answer, but what does it mean?</p>


<ul>
<li><em>i</em> is a “new imaginary dimension” to measure a number</li>
<li><em>i</em> (or <em>-i</em>) is what numbers “become” when rotated</li>
<li>Multiplying <em>i</em> is a rotation by 90 degrees counter-clockwise </li>
<li>Multiplying by <em>-i</em> is a rotation of 90 degrees clockwise</li>
<li>Two rotations in either direction is -1: it brings us back into the “regular” dimensions of positive and negative numbers.</li>
</ul>



<p><strong>Numbers are 2-dimensional.</strong> Yes, it’s mind bending, just like decimals or long division would be mind-bending to an ancient Roman. (<i>What do you mean there’s a number between 1 and 2?</i>). It’s a strange, new way to think about math.</p>

<p>We asked “How do we turn 1 into -1 in two steps?” and found an answer: rotate it 90 degrees. <strong>It’s a strange, new way to think about math.</strong> But it’s useful. (By the way, this geometric interpretation of complex numbers didn’t arrive until decades after <em>i</em> was discovered).</p>

<p>Also, keep in mind that having counter-clockwise be positive is a human convention — it easily could have been the other way.</p>

<h2>Finding Patterns</h2>

<p>Let’s dive into the details a bit. When multiplying negative numbers (like -1), you get a pattern:</p>


<ul>
<li>1, -1, 1, -1, 1, -1, 1, -1</li>
</ul>



<p>Since -1 doesn’t change the <strong>size</strong> of a number, just the sign, you flip back and forth. For some number “x”, you’d get:</p>


<ul>
<li>x, -x, x, -x, x, -x…</li>
</ul>



<p>This idea is useful. The number “x” can represent a good or bad hair week. Suppose weeks alternate between good and bad; this is a good week; what will it be like in 47 weeks?</p>

<p><img src="http://betterexplained.com/latexrender/pictures/1e2f373a946134b74cce52595c3fbce6.gif" title="\displaystyle{x \cdot -1^{47} = x \cdot -1 = -x}" alt="\displaystyle{x \cdot -1^{47} = x \cdot -1 = -x}"></p>

<p>So -x means a bad hair week. Notice how negative numbers “keep track of the sign” — we can throw -1^47 into a calculator without having to count (”<em>Week 1 is good, week 2 is bad… week 3 is good…</em>“). Things that <strong>flip back and forth can be modeled well with negative numbers</strong>.</p>

<p>Ok. Now what happens if we keep multiplying by <em>i</em>?</p>

<p><img src="http://betterexplained.com/latexrender/pictures/9461aa81c7578d0bc8f8f2bee8803171.gif" title="\displaystyle{1, i, i^2, i^3, i^4, i^5...}" alt="\displaystyle{1, i, i^2, i^3, i^4, i^5...}"></p>

<p>Very funny. Let’s reduce this a bit:</p>


<ul>
<li><img src="http://betterexplained.com/latexrender/pictures/1601319a5332024d6c12b37d6ee25478.gif" title="\displaystyle{1 = 1}" alt="\displaystyle{1 = 1}"> (No questions here)</li>
<li><img src="http://betterexplained.com/latexrender/pictures/55a107b11304f260a99541dd28b14be5.gif" title="\displaystyle{i = i}" alt="\displaystyle{i = i}"> (Can’t do much)</li>
<li><img src="http://betterexplained.com/latexrender/pictures/7629fbf1f6929348712245765c35727a.gif" title="\displaystyle{i^2 = -1}" alt="\displaystyle{i^2 = -1}"> (That’s what <em>i</em> is all about)</li>
<li><img src="http://betterexplained.com/latexrender/pictures/dafa4bde0164fe6c456aed9db098a357.gif" title="\displaystyle{i^3 = (i \cdot i) \cdot i = -1 \cdot i = -i}" alt="\displaystyle{i^3 = (i \cdot i) \cdot i = -1 \cdot i = -i}"> (Ah, 3 rotations clockwise = 1 rotation counter-clockwise. Neat.)</li>
<li><img src="http://betterexplained.com/latexrender/pictures/5344f72e70ae67dd415c7cf0b9bc4676.gif" title="\displaystyle{i^4 = (i \cdot i) \cdot (i \cdot i) = -1 \cdot -1 = 1}" alt="\displaystyle{i^4 = (i \cdot i) \cdot (i \cdot i) = -1 \cdot -1 = 1}"> (4 rotations bring us “full circle”)</li>
<li><img src="http://betterexplained.com/latexrender/pictures/f4eb0b0bc9d881f82be7683184edfba4.gif" title="\displaystyle{i^5 = i^4 \cdot i = 1 \cdot i = i}" alt="\displaystyle{i^5 = i^4 \cdot i = 1 \cdot i = i}"> (Here we go again…)</li>
</ul>



<p>Represented visually:</p>

<p><img src="http://betterexplained.com/wp-content/uploads/complex/imaginary_cycle.png" alt="imaginary number cycle"></p>

<p>We cycle every 4th rotation. This makes sense, right? Any kid can tell you that 4 left turns is the same as no turns at all. Now rather than focusing on imaginary numbers (i, i^2), look at the general pattern:</p>


<ul>
<li>X, Y, -X, -Y, X, Y, -X, -Y…</li>
</ul>



<p>Like negative numbers modeling flipping, imaginary numbers <strong>can model anything that rotates</strong> between two dimensions “X” and “Y”. Or anything with a <strong>cyclic, circular relationship</strong> — have anything in mind?</p>

<p>‘Cos it’d be a sin if you didn’t. There’ll <a href="http://en.wikipedia.org/wiki/De_Moivre&#39;s_formula"><del>de Moivre</del></a> be more in future articles. <em>[Editor’s note: Kalid is in electroshock therapy to treat his pun addiction.]</em></p>

<h2>Understanding Complex Numbers</h2>

<p>There’s another detail to cover: can a number be both “real” and “imaginary”? </p>

<p>You bet. Who says we have to rotate the entire 90 degrees? If we keep 1 foot in the “real” dimension and another in the imaginary one, it looks like this:</p>

<p><img src="http://betterexplained.com/wp-content/uploads/complex/one_plus_i.png" alt="imaginary number i plus i"></p>

<p>We’re at a 45 degree angle, with equal parts in the real and imaginary (1 + i). It’s like a hotdog with both mustard and ketchup — who says you need to choose?</p>

<p>In fact, we can pick any combination of real and imaginary numbers and make a triangle. The angle becomes the “angle of rotation”. A <strong>complex number</strong> is the fancy name for numbers with both real and imaginary parts. They’re written a + bi, where</p>


<ul>
<li>a is the real part</li>
<li>b is the imaginary part</li>
</ul>



<p><img src="http://betterexplained.com/wp-content/uploads/complex/a_plus_bi.png" alt="imaginary number a plus bi"></p>

<p>Not too bad. But there’s one last question: how “big” is a complex number? We can’t measure the real part or imaginary parts in isolation, because that would miss the big picture.</p>

<p>Let’s step back. The size of a negative number is not whether you can count it — it’s the distance from zero. In the case of negatives this is:</p>

<p><img src="http://betterexplained.com/latexrender/pictures/9ae91fcef1e34f19e92fbcf831f18041.gif" title="\displaystyle{Size \hspace{2pt} of \hspace{2pt} (-x) = \sqrt{(-x)^2} = |x|}" alt="\displaystyle{Size \hspace{2pt} of \hspace{2pt} (-x) = \sqrt{(-x)^2} = |x|}"></p>

<p>Which is another way to find the absolute value. But for complex numbers, how do we measure two components at 90 degree angles?</p>

<p><em>It’s a bird… it’s a plane… it’s Pythagoras!</em></p>

<p>Geez, his theorem shows up <a href="http://betterexplained.com/articles/measure-any-distance-with-the-pythagorean-theorem/">everywhere</a>, even in numbers invented 2000 years after his time. Yes, we are making a triangle of sorts, and the hypotenuse is the distance from zero:</p>

<p><img src="http://betterexplained.com/latexrender/pictures/c81c999ca86729d2bde9f9e8dfda76f9.gif" title="\displaystyle{Size \hspace{2pt} of \hspace{2pt} a + bi = \sqrt{a^2 + b^2}}" alt="\displaystyle{Size \hspace{2pt} of \hspace{2pt} a + bi = \sqrt{a^2 + b^2}}"></p>

<p>Neat. While measuring the size isn’t as easy as “dropping the negative sign”, complex numbers do have their uses. Let’s take a look.</p>

<h2>A Real Example: Rotations</h2>

<p>We’re not going to wait until college physics to use imaginary numbers. Let’s try them out <strong>today</strong>. There’s much more to say about complex multiplication, but keep this in mind:</p>


<ul>
<li>Multiplying by a complex number rotates by its angle</li>
</ul>



<p>Let’s take a look. Suppose I’m on a boat, with a heading of 3 units East for every 4 units North. I want to change my heading 45 degrees counter-clockwise. What’s the new heading?</p>

<p><img src="http://betterexplained.com/wp-content/uploads/complex/imaginary_example1.png" alt="imaginary number example" style="margin:0 auto;text-align:center"></p>

<p>Some hotshot will say “<em>That’s simple! Just take the sine, cosine, gobbledegook by the tangent… fluxsom the foobar… and…</em>“. <strong><em>Crack</em></strong>. Sorry, did I break your calculator? Care to answer that question again?</p>

<p>Let’s try a simpler approach: we’re on a heading of 3 + 4i (whatever that angle is; we don’t really care), and want to rotate by 45 degrees. Well, 45 degrees is 1 + i, so we can multiply by that amount!</p>

<p><img src="http://betterexplained.com/wp-content/uploads/complex/imaginary_example2.png" alt="imaginary number example"></p>

<p>Here’s the idea:</p>


<ul>
<li>Original heading: 3 units East, 4 units North = 3 + 4i</li>
<li>Rotate clockwise by 45 degrees = multiply by 1 + i</li>
</ul>



<p>If we multiply them together we get:</p>

<p><img src="http://betterexplained.com/latexrender/pictures/e93691324f2121cb547ac3437ed73988.gif" title="\displaystyle{(3 + 4i) \cdot (1 + i) = 3 + 4i + 3i + 4i^2 = 3 - 4 + 7i = -1 + 7i}" alt="\displaystyle{(3 + 4i) \cdot (1 + i) = 3 + 4i + 3i + 4i^2 = 3 - 4 + 7i = -1 + 7i}"></p>

<p>So our new orientation is 1 unit West (-1 East), and 7 units North, which you could draw out and follow.</p>

<p>But yowza! We found that out in 10 seconds, without touching sine or cosine. There were no vectors, matrices, or keeping track what quadrant we are in. It was <strong>just arithmetic</strong> with a touch of algebra to cross-multiply. Imaginary numbers have the rotation rules baked in: <strong>it just works.</strong></p>

<p>Even better, the result is <strong>useful</strong>. We have a heading (-1, 7) instead of an angle (atan(7/-1) = 98.13, keeping in mind we’re in quadrant 2). How, exactly, were you planning on drawing and following that angle? With the protractor you keep around?</p>

<p>No, you’d convert it into cosine and sine (-.14 and .99), find a reasonable ratio between them (about 1 to 7), and sketch out the triangle. Complex numbers beat you to it, instantly, accurately, and without a calculator.</p>

<p>If you’re like me, you’ll find this use <strong>mind-blowing</strong>. And if you don’t, well, I’m afraid math doesn’t toot your horn. Sorry.</p>

<p>Trigonometry is great, but complex numbers can make ugly calculations simple (like calculating cosine(a+b) ). This is just a preview; later articles will give you the full meal.</p>

<h2>Complex Numbers Aren’t</h2>

<p>That was a whirlwind tour of my basic insights. Take a look at the first chart — it should make sense now.</p>

<p>There’s <strong>so much more</strong> to these beautiful, zany numbers, but my brain is tired. My goals were simple:</p>


<ul>
<li>Convince you that complex numbers were considered “crazy” but can be useful (just like negative numbers were)</li>
<li>Show how complex numbers can make certain problems easier, like rotations</li>
</ul>



<p>If I seem hot and bothered about this topic, there’s a reason. Imaginary numbers have been a bee in my bonnet for <strong>years</strong> — the lack of an intuitive insight frustrated me.</p>

<p>Now that I’ve finally had insights, I’m bursting to share them. But it frustrates me that you’re reading this on the blog of a wild-eyed lunatic, and <strong>not</strong> in a classroom. We suffocate our questions and “chug through” — because we don’t search for and share clean, intuitive insights. Egad.</p>

<p>But better to light a candle than curse the darkness: here’s my thoughts, and one of you will shine a spotlight. Thinking we’ve “figured out” a topic like numbers is what keeps us in Roman Numeral land.</p>

<p>There’s much more complex numbers: check out the <a href="http://betterexplained.com/articles/intuitive-arithmetic-with-complex-numbers/">details of complex arithmetic</a>. Happy math.</p>

<h2>Epilogue: But they’re still strange!</h2>

<p>I know, they’re still strange to me too. I try to put myself in the mind of the first person to discover zero.</p>

<p>Zero is such a weird idea, having “something” represent “nothing”, and it eluded the Romans. Complex numbers are similar — it’s a new way of thinking. But both zero and complex numbers make math much easier. If we never adopted strange, new number systems, we’d still be counting on our fingers.</p>

<p>I repeat this analogy because it’s <strong>so easy</strong> to start thinking that complex numbers aren’t “normal”. Let’s keep our mind open: in the future they’ll chuckle that complex numbers were once distrusted, even until the 2000’s.</p>

<p>If you want more nitty-gritty, check out <a href="http://en.wikipedia.org/wiki/Imaginary_number">wikipedia</a>, the <a href="http://mathforum.org/dr.math/faq/faq.imag.num.html">Dr. Math discussion</a>, or another argument on <a href="http://www.math.toronto.edu/mathnet/answers/imagexist.html">why imaginary numbers exist</a>. </p><img src="http://feeds.feedburner.com/~r/Betterexplained/~4/203914123" height="1" width="1"></div>
</div>

<div class="feed">
  <div class="title">56. <a href="http://feeds.feedburner.com/~r/Betterexplained/~3/210308375/" target="_blank">Intuitive Arithmetic With Complex Numbers</a></div>
  <div class="content"><p> Imaginary numbers have an <a href="http://betterexplained.com/articles/a-visual-intuitive-guide-to-imaginary-numbers/">intuitive explanation</a>: they “rotate” numbers, just like negatives make a “mirror image” of a number. This insight makes arithmetic with complex numbers easier to understand, and is a great way to double-check your results. Here’s our cheatsheet:</p>

<p><img src="http://betterexplained.com/wp-content/uploads/complex/complex_operations_table.png" alt="complex arithmetic operations" title="complex arithmetic operations"></p>

<p>This post will walk through the intuitive meanings.</p>

<h2>Complex Variables</h2>

<p>In regular algebra, we often say “x = 3″ and all is dandy — there’s some number “x”, whose value is 3. With complex numbers, there’s a gotcha: there’s two dimensions to talk about. When writing</p>

<p><img src="http://betterexplained.com/latexrender/pictures/405393747430b7d9492de979d30863ee.gif" title="\displaystyle{z = 3 + 4i}" alt="\displaystyle{z = 3 + 4i}"></p>

<p><img src="http://betterexplained.com/wp-content/uploads/complex/complex_a_plus_bi.png" alt="complex number example" title="complex number example"></p>

<p>we’re saying there’s a number “z” with two parts: 3 (the real part) and 4i (imaginary part). It is a bit strange how “one” number can have two parts, but we’ve been doing this for a while. We often write:</p>

<p><img src="http://betterexplained.com/latexrender/pictures/11c6cc24036558c84351ad4af04b4d4c.gif" title="\displaystyle{y = 3\frac{4}{10} = 3 + .4}" alt="\displaystyle{y = 3\frac{4}{10} = 3 + .4}"></p>

<p>and it doesn’t bother us that a single number “y” has both an integer part (3) and a fractional part (.4 or 4/10). Y is a combination of the two. Complex numbers are similar: they have their real and imaginary parts “contained” in a single variable (shorthand is often Re and Im).</p>

<p>Unfortunately, we don’t have nice notation like (3.4) to “merge” the parts into a single number. I had an idea to write the imaginary part vertically, in fading ink, but it wasn’t very popular. So we’ll stick to the “a + bi” format.</p>

<h2>Measuring Size</h2>

<p>Because complex numbers use two independent axes, we find size (magnitude) using the Pythagorean Theorem:</p>

<p><img src="http://betterexplained.com/wp-content/uploads/complex/complex_magnitude.png" alt="complex magnitude" title="complex magnitude"></p>

<p>So, a number z = 3 + 4i would have a magnitude of 5. The shorthand for “magnitude of z” is this: |z|</p>

<p>See how it looks like the absolute value sign? Well, in a way, it is. Magnitude measures a complex number’s “distance from zero”, just like absolute value measures a negative number’s “distance from zero”.</p>

<h2>Complex Addition and Subtraction</h2>

<p>We’ve seen that <a href="http://betterexplained.com/articles/rethinking-arithmetic-a-visual-guide/">regular addition</a> can be thought of as “sliding” by a number. Addition with complex numbers is similar, but we can slide in two dimensions (real or imaginary). For example:</p>

<p><img src="http://betterexplained.com/wp-content/uploads/complex/complex_addition.png" alt="complex addition" title="Adding complex numbers"></p>

<p>Adding (3 + 4i) to (-1 + i) gives 2 + 5i.</p>

<p>Again, this is a visual interpretation of how “independent components” are combined: we track the real and imaginary parts separately.</p>

<p>Subtraction is the reverse of addition — it’s sliding in the <strong>opposite</strong> direction. Subtracting (1 + i) is the same as adding -1 * (1 + i), or adding (-1 - i).</p>

<h2>Complex Multiplication</h2>

<p>Here’s where the math gets interesting. When we multiply two complex numbers (x and y) to get z:</p>


<ul>
<li><strong>Add the angles:</strong> angle(z) = angle(x) + angle(y)</li>
<li><strong>Multiply the magnitudes:</strong> |z| = |x| * |y|</li>
</ul>



<p>That is, the angle of z is the sum of the angles of x and y, and the magnitude of z is the product of the magnitudes. Believe it or not, the magic of complex numbers makes the math work out!</p>

<p>Multiplying by the magnitude (size) makes sense — we’re used to that happening in regular multiplication (3 × 4 means you multiply 3 by 4’s size). The reason the angle addition works is more detailed, and we’ll save it for another time. (Curious? Find the sine and cosine <a href="http://www.cut-the-knot.org/proofs/sine_cosine.shtml">addition formulas</a> and compare them to how (a + bi) * (c + di) get multiplied out).</p>

<p>Time for an example: let’s multiply z = 3 + 4i by itself. Before doing all the math, we know a few things:</p>


<ul>
<li>The resulting magnitude will be 25. z has a magnitude of 5, so |z| * |z| = 25.</li>
<li>The resulting angle will be above 90. 3 + 4i is above 45 degrees (since 3 + 3i would be 45 degrees), so twice that angle will be more than 90.</li>
</ul>



<p>With our predictions on paper, we can do the math:</p>

<p><img src="http://betterexplained.com/latexrender/pictures/2b779cc84ad6e280191f2e8776361cd4.gif" title="\displaystyle{(3 + 4i) * (3 + 4i) = 9 + 16i^2 + 24i = -7 + 24i}" alt="\displaystyle{(3 + 4i) * (3 + 4i) = 9 + 16i^2 + 24i = -7 + 24i}"></p>

<p>Time to check our results:</p>


<ul>
<li>Magnitude: sqrt((-7 * -7) + (24 * 24)) = sqrt(625) = 25, which matches our guess.</li>
<li>Angle: Since -7 is negative and 24i is positive, we know we are going “backwards and up”, which means we’ve crossed 90 degrees (”straight up”). Getting geeky, we compute atan(24/-7) = 106.2 degrees (keeping in mind we’re in quadrant 2). This guess checks out too.</li>
</ul>



<p>Nice. While we can always do the math out, the intuition about rotations and scaling helps us check the result. If the resulting angle was less than 90 (”forward and up”, for example), or the resulting magnitude not 25, we’d know there was a mistake in our math.</p>

<h2>Complex Division</h2>

<p>Division is the opposite of multiplication, just like subtraction is the opposite of addition. When dividing complex numbers (x divided by y), we:</p>


<ul>
<li><strong>Subtract angles</strong> angle(z) = angle(x) - angle(y)</li>
<li><strong>Divide by magnitude</strong> |z| = |x| / |y|</li>
</ul>



<p>Sounds good. Now let’s try to do it:</p>

<p><img src="http://betterexplained.com/latexrender/pictures/3590978cb1a0ba177f2621cf60cfbd68.gif" title="\displaystyle{\frac{3 + 4i}{1 + i}}" alt="\displaystyle{\frac{3 + 4i}{1 + i}}"></p>

<p>Hrm. Where to start? How do we actually <strong>do</strong> the division? Dividing regular algebraic numbers gives me the creeps, let alone weirdness of i (<i>Mister mister! Didya know that 1/i = -i? Just multiply both sides by i and see for yourself!</i> Eek.). Luckily there’s a shortcut.</p>

<h2>Introducing Complex Conjugates</h2>

<p>Our first goal of division is to subtract angles. How do we do this? Multiply by the opposite angle! This will “add” a negative angle, doing an angle subtraction.</p>

<p><img src="http://betterexplained.com/wp-content/uploads/complex/complex_conjugates.png" alt="complex conjugate example" title="complex conjugate example"></p>

<p>Instead of z = a + bi, think about a number z* = a - bi, called the “complex conjugate”. It has the same real part, but is the “mirror image” in the imaginary dimension. The conjugate or “imaginary reflection” has the same magnitude, but the opposite angle!</p>

<p>So, multiplying by a - bi is the same as <strong>subtracting</strong> an angle. Neato.</p>

<p><img src="http://betterexplained.com/wp-content/uploads/complex/conjugate_multiply.png" alt="multiply complex conjugate" title="multiply complex conjugate"></p>

<p>Complex conjugates are indicated by a star (z*) or bar above the number — mathematicians love to argue about these notational conventions. Either way, the conjugate is the complex number with the imaginary part flipped:</p>

<p><img src="http://betterexplained.com/latexrender/pictures/6ca7113efe8b04cb02a5c221965063df.gif" title="\displaystyle{z = a + bi}" alt="\displaystyle{z = a + bi}"> has complex conjugate <img src="http://betterexplained.com/latexrender/pictures/65fd56d5f49b4e8aca9a71c7bc74dbbb.gif" title="\displaystyle{z^* = \bar{z} = a - bi}" alt="\displaystyle{z^* = \bar{z} = a - bi}"></p>

<p>Note that b doesn’t have to be “negative”. If z = 3 - 4i, then z* = 3 + 4i.</p>

<h2>Multiplying By the Conjugate</h2>

<p>What happens if you multiply by the conjugate? What is z times z*? Without thinking, think about this:</p>

<p><img src="http://betterexplained.com/latexrender/pictures/cc51fdd0eeb8d2ffa9b54767644d6f4d.gif" title="\displaystyle{z \cdot z^* = 1 \cdot z \cdot z^*}" alt="\displaystyle{z \cdot z^* = 1 \cdot z \cdot z^*}"></p>

<p>So we take 1 (a real number), add angle(z), and add angle (z*). But this last angle is negative — it’s a subtraction! So our final result should be a real number, since we’ve canceled the angles. The number should be |z|^2 since we scaled by the size twice.</p>

<p>Now let’s do an example: <img src="http://betterexplained.com/latexrender/pictures/82737b674b3410bae7e8af232d51e147.gif" title="\displaystyle{(3 + 4i) * (3 - 4i) = 9 - 16i^2 = 25}" alt="\displaystyle{(3 + 4i) * (3 - 4i) = 9 - 16i^2 = 25}"></p>

<p>We got a real number, like we expected! The math fans can try the algebra also:</p>

<p><img src="http://betterexplained.com/latexrender/pictures/5897488936186cfd4a35febea34aaafc.gif" title="\displaystyle{(a + bi) * (a - bi) = a^2 + abi - abi -b^2i^2 = a^2 + b^2 }" alt="\displaystyle{(a + bi) * (a - bi) = a^2 + abi - abi -b^2i^2 = a^2 + b^2 }"></p>

<p>Tada! The result has no imaginary parts, and is the magnitude squared. Understanding complex conjugates as a “negative rotation” lets us predict these results in a different way.</p>

<h2>Scaling Your Numbers</h2>

<p>When multiplying by a conjugate z*, we scale by the magnitude |z*|. To reverse this effect we can divide by |z|, and to actually <strong>shrink</strong> by |z| we have to divide again. All in all, we have to divide by |z| * |z| to the original number after multiplying by the conjugate.</p>

<h2>Show Me The Division!</h2>

<p>I’ve been sidestepping the division, and here’s the magic. If we want to do</p>

<p><img src="http://betterexplained.com/latexrender/pictures/3590978cb1a0ba177f2621cf60cfbd68.gif" title="\displaystyle{\frac{3 + 4i}{1 + i}}" alt="\displaystyle{\frac{3 + 4i}{1 + i}}"></p>

<p>We can approach it intuitively:</p>


<ul>
<li>Rotate by opposite angle: multiply by (1 - i) instead of (1 + i)</li>
<li>Divide by magnitude squared: divide by |sqrt(2)|^2 = 2</li>
</ul>



<p>The answer, using this approach, is:</p>

<p><img src="http://betterexplained.com/latexrender/pictures/1ba70378087236c5fae8a66717f0aeef.gif" title="\displaystyle{\frac{3 + 4i}{1 + i} = (3 + 4i) \cdot (1 - i) \cdot \frac{1}{2} = (3 - 4i^2 + 4i - 3i) \cdot \frac{1}{2} = \frac{7}{2} + \frac{1}{2}i}" alt="\displaystyle{\frac{3 + 4i}{1 + i} = (3 + 4i) \cdot (1 - i) \cdot \frac{1}{2} = (3 - 4i^2 + 4i - 3i) \cdot \frac{1}{2} = \frac{7}{2} + \frac{1}{2}i}"></p>

<p>The more traditional “plug and chug” method is to multiply top and bottom by the complex conjugate:</p>

<p><img src="http://betterexplained.com/latexrender/pictures/45062572293b18a3c9525d97ad853b30.gif" title="\displaystyle{\frac{3 + 4i}{1 + i} = \frac{3 + 4i}{1 + i} \cdot \frac{1 - i}{1 - i} = \frac{3 - 4i^2 + 4i - 3i}{1 - i^2} = \frac{7 + i}{2}}" alt="\displaystyle{\frac{3 + 4i}{1 + i} = \frac{3 + 4i}{1 + i} \cdot \frac{1 - i}{1 - i} = \frac{3 - 4i^2 + 4i - 3i}{1 - i^2} = \frac{7 + i}{2}}"></p>

<p>We’re traditionally taught to “just multiply both sides by the complex conjugate” without questioning what complex division really means. But not today.</p>

<p>We know what’s happening: division is subtracting an angle and shrinking the magnitude. By multiplying top and bottom by the conjugate, we subtract by the angle of (1-i), which happens to make the denominator a real number (it’s no coincidence, since it’s the exact opposite angle). We scaled both the top and bottom by the same amount, so the effects cancel. The result is to turn division into a multiplication in the numerator.</p>

<p>Both approaches work (you’re usually taught the second), but it’s nice to have one to double-check the other.</p>

<h2>More Math Tricks</h2>

<p>Now that we understand the conjugate, there’s a few properties to consider:</p>

<p><img src="http://betterexplained.com/latexrender/pictures/442250db48332d0877ab5addee57979a.gif" title="\displaystyle{(x + y)^* = x^* + y^*}" alt="\displaystyle{(x + y)^* = x^* + y^*}"></p>

<p><img src="http://betterexplained.com/latexrender/pictures/223fb483af561f93ebda3224c73c279c.gif" title="\displaystyle{(x \cdot y)^* = x^* \cdot y^*}" alt="\displaystyle{(x \cdot y)^* = x^* \cdot y^*}"></p>

<p>The first should make sense. Adding two numbers and “reflecting” (conjugating) the result, is the same as adding the reflections. Another way to think about it: sliding two numbers then taking the opposite, is the same as sliding both times in the opposite direction.</p>

<p>The second property is trickier. Sure, the <a href="http://mathworld.wolfram.com/ComplexConjugate.html">algebra may work</a>, but what’s the intuitive explanation?</p>

<p>The result (xy)* means:</p>


<ul>
<li>Multiply the magnitudes: |x| * |y|</li>
<li>Add the angles and take the conjugate (opposite): angle(x) + angle(y) becomes “-angle(x) + -angle(y)”</li>
</ul>



<p>And x* times y*  means: </p>


<ul>
<li>Multiply the magnitudes: |x| * |y| (this is the same as above)</li>
<li>Add the conjugate angles: angle(x*) + angle(y*) = -angle(x) + -angle(y)</li>
</ul>



<p>Aha! We get the same angle and magnitude in each case, and we didn’t have to jump into the traditional algebra explanation. Algebra is fine, but it isn’t always the most satisfying explanation.</p>

<h2>A Quick Example</h2>

<p>The conjugate is a way to “undo” a rotation. Think about it this way:</p>


<ul>
<li>I deposited $3, $10, $15.75 and $23.50 into my account. What transaction will cancel these out? To find the opposite: add them up, and multiply by -1.</li>
<li>I rotated a line by doing several multiplications: (3 + 4i), (1 + i), and (2 + 10i). What rotation will cancel these out? To find the opposite: multiply the complex numbers together, and take the conjugate of the result.</li>
</ul>



<p>See the conjugate z* as a way to “cancel” the rotation effects of z, just like a negative number “cancels” the effects of addition. One caveat: with conjugates, you need to divide by |z| * |z| to remove the scaling effects as well.</p>

<h2>Closing Thoughts</h2>

<p>The math here isn’t new, but I never realized <strong>why</strong> complex conjugates worked as they did. Why a - bi and not -a + bi? Well, complex conjugates are not a random choice, but a mirror image from the imaginary perspective, with the exact opposite angle.</p>

<p>Seeing imaginary numbers as rotations gives us a <a href="http://betterexplained.com/articles/how-to-develop-a-mindset-for-math/">new mindset</a> to approach problems; the “plug and chug” formulas can make intuitive sense, even for a strange topic like complex numbers. Happy math. </p><img src="http://feeds.feedburner.com/~r/Betterexplained/~4/210308375" height="1" width="1"></div>
</div>

<div class="feed">
  <div class="title">57. <a href="http://feeds.feedburner.com/~r/Betterexplained/~3/216296180/" target="_blank">A Visual Guide to Simple, Compound and Continuous Interest Rates</a></div>
  <div class="content"><p> Interest rates are confusing, despite their ubiquity. This post takes an in-depth look at why interest rates behave as they do.</p>

<p>Understanding these concepts will help understand finance (mortgages &amp; savings rates), along with the <a href="http://betterexplained.com/articles/an-intuitive-guide-to-exponential-functions-e/">omnipresent e</a> and <a href="http://betterexplained.com/articles/demystifying-the-natural-logarithm-ln/">natural logarithm</a>. Here’s our cheatsheet:</p>

<table width="100%" border="1px solid #ccc" cellspacing="0">
<tr>
	<th>Term</th>
	<th>Return</th>
	<th>Description &amp; Usage</th>
</tr>
<tr>
	<td>Simple</td>
	<td><img src="http://betterexplained.com/latexrender/pictures/9664457993e5a6b2ea2b1ccd014dc9d0.gif" title="\displaystyle{P \cdot r \cdot n}" alt="\displaystyle{P \cdot r \cdot n}"></td>
	<td>Fixed, non-growing return (bond coupons)</td>
</tr>
<tr>
	<td>Compound <br>(Annual)</td>
	<td><img src="http://betterexplained.com/latexrender/pictures/ad1f9b68a27431f0f1c6c7f786cbbcd1.gif" title="\displaystyle{P \cdot (1 + r)^n}" alt="\displaystyle{P \cdot (1 + r)^n}"></td>
	<td>Changes each year (stock market, inflation)</td>
</tr>
<tr>
	<td>Compound <br>(t times per year)</td>
	<td><img src="http://betterexplained.com/latexrender/pictures/6d403935cbc9b0e67801b39ff8ca0046.gif" title="\displaystyle{P \cdot (1 + r/t)^{tn}}" alt="\displaystyle{P \cdot (1 + r/t)^{tn}}"></td>
	<td>Changes each month/week/day (savings account)</td>
</tr>
<tr>
	<td>Continuous Growth</td>
	<td><img src="http://betterexplained.com/latexrender/pictures/a5c871225b4ff9ac3b87bca8dc691973.gif" title="\displaystyle{P \cdot e^{rt}}" alt="\displaystyle{P \cdot e^{rt}}"></td>
	<td>Changes each instant (radioactive decay, temperature)</td>
</tr>
<tr>
<td><span>APR</span></td>
<td colspan="2">Annual Percentage <strong>Rate</strong> (compounding not included)</td>
</tr>
<tr>
<td><span>APY</span></td>
<td colspan="2">Annual Percentage <strong>Yield</strong> (all compounding effects included)</td>
</tr>
</table>

<p>And a quick calculator to convert <span>APR </span>to <span>APY</span>:</p>

<p></p>

<h2>Why the fuss?</h2>

<p>Interest rates are complex. Like Roman numerals and hieroglyphics, our first system “worked” but wasn’t quite ideal.</p>

<p>In the beginning, you might have had 100 gold coins and were paid 12% per year (percent = per cent = per hundred — those Roman numerals still show up!). It’s simple enough: we get 12 coins a year. But is it really 12?</p>

<p>If we break it down, it seems we earn 1 gold a month: 6 for January-June, and 6 for July-December. But wait a minute — after our June payout we’d have 106 gold in July, and yet earn only 6 during the rest of the year? Are you saying 100 and 106 earn the same amount in 6 months? By that logic, do 100 and 200 earn the same amount, too? Uh oh.</p>

<p>This issue didn’t seem to bother the ancient Egyptians, but did raise questions in the 1600s and led to <a href="http://en.wikipedia.org/wiki/Interest#Interest_in_mathematics">Bernoulli’s discovery of e</a> (sorry math fans, e wasn’t discovered via some hunch that a strange limit would have useful properties). There’s much to say about this riddle — just keep this in mind as we dissect interest rates:</p>


<ul>
<li><strong>Interest rates and terminology were invented before the idea of compounding.</strong> Heck, loans were around in 1500 <span>BC, </span>before exponents, 0, or even the decimal point! So it’s no wonder our discussions can get confusing.</li>
<li><strong>Nature doesn’t wait for a human year before changing</strong>. Interest earnings are a type of “growth”, but natural phenomena like temperature and radioactive decay change constantly, every second and faster. This is one reason why physics equations model change with “e” and not “(1+r)^n”: Nature rudely ignores our calendar when making adjustments.</li>
</ul>



<h2>Learn the Lingo</h2>

<p>As a result of these complications, we need a few terms to discuss interest rates:</p>


<ul>
<li><strong><span>APR </span>(annual percentage rate):</strong> The rate someone tells you (”12% per year!”). You’ll see this as “r” in the formula.</li>
<li><strong><span>APY </span>(annual percentage yield):</strong> The rate you actually get after a year, after all compounding is taken into account. You can consider this “total return” in the formula. The <span>APY </span>is greater than or equal to the <span>APR.</span></li>
</ul>



<p><span>APR </span>is what the bank tells you, the <span>APY </span>is what you pay (the price after taxes, shipping and handling, if you get my drift). And of course, banks advertise the rate that looks better.</p>

<p>Getting a credit card or car loan? They’ll show the “low <span>APR</span>” you’re paying, to hide the higher <span>APY.</span> But opening a savings account? Well, of course they’d tout the “high <span>APY</span>” they’re paying to look generous. </p>

<p><strong>The <span>APY </span>(actual yield) is what you care about, and the way to compare competing offers</strong>.</p>

<h2>Simple Interest</h2>

<p>Let’s start on the ground floor: <strong>Simple interest pays a fixed amount over time</strong>. A few examples:</p>


<ul>
<li>Aesop’s fable of the golden goose: every day it laid a single golden egg. It couldn’t lay faster, and the eggs didn’t grow into golden geese of their own.</li>
<li>Corporate bonds: A bond with a face value of $1000 and 5% interest rate (coupon) pays you $50 per year until it expires. You can’t increase the face value, so $50/year what you will get from the bond. (In reality, the bond would pay $25 every 6 months).</li>
</ul>



<p>Simple interest is the most <strong>basic type of return</strong>. Depositing $100 into an account with 50% simple (annual) interest looks like this:</p>

<p><img src="http://betterexplained.com/wp-content/uploads/interest/simple1.png" alt="simple interest" title="simple interest" width="500" height="286" border="0"></p>

<p>You start with a principal (aka investment) of $100 and earn $50 each year. I imagine the blue principal “shoveling” green money upwards every year.</p>

<p>However, this new, green money is stagnant — it can’t grow! With simple interest, the $50 just sits there. Only the original $100 can do “work” to generate money.</p>

<p>Simple interest has a simple formula: Every period you earn P * r (principal * interest rate). After n periods you have:</p>

<p><img src="http://betterexplained.com/latexrender/pictures/8abcabce13f424c84ebb289f27d37bad.gif" title="\displaystyle{return = P \cdot r \cdot n}" alt="\displaystyle{return = P \cdot r \cdot n}"></p>

<p>This formula works as long as “r” and “n” refer to the same time period. It could be years, months, or days — though in most cases, we’re considering annual interest. There’s no trickery because there’s no compounding — interest can’t grow.</p>

<p>Simple interest is useful when:</p>


<ul>
<li><strong>Your interest earnings create something that cannot grow more</strong>. It’s like the golden goose creating eggs, or a corporate bond paying money that cannot be reinvested.</li>
<li><strong>You want simple, predictable, non-exponential results</strong>. Suppose you’re encouraging your kids to save. You could explain that you’ll put aside $1/month in “fun money” for every $20 in their piggybank. Most kids would be thrilled and buy comic books each month. If your last name is Greenspan, your kid might ask to reinvest the dividend.</li>
</ul>



<p>In practice, simple interest is fairly rare because most types of earnings can be reinvested. There really isn’t an <span>APR </span>vs <span>APY </span>distinction, since your earnings can’t change: you always earn the same amount per year.</p>

<h2>Really Understanding Growth</h2>

<p>Most interest explanations stop there: here’s the formula, now get on your merry way. Not here: let’s see what’s really happening.</p>

<p>First, what does an interest rate mean? <strong>I think of it as a type of “speed”</strong>:</p>


<ul>
<li><strong>50 mph</strong> means you’ll travel 50 miles in the course of an hour</li>
<li><strong>r = 50% per year</strong> means you’ll earn 50% of your principal in the course of a year. If P = $100, you’ll earn $50/year (your “speed of money growth”). </li>
</ul>



<p>But both types of speed have a subtlety: <strong>we don’t have to wait the full time period!</strong></p>

<p>Does driving 50 mph mean you must go a full hour? No way! You can drive “only” 30 minutes and go 25 miles (50 mph * .5 hours). You could drive 15 minutes and go 12.5 miles (50 mph * .25 hours). You get the idea.</p>

<p>Interest rates are similar. An interest rate gives you a “trajectory” or “pace” to follow. If you have $100 at a 50% simple interest rate, your pace is $50/year. But you don’t need to follow that pace for a full year! If you grew for 6 months, you should be entitled to $25. Take a look at this:</p>

<p><img src="http://betterexplained.com/wp-content/uploads/interest/simple_interest_trajectory.png" alt="simple interest trajectory" title="simple interest trajectory" width="500" height="286" border="0"></p>

<p>We start with $100, in blue. Each year that blue contributes $50 (in green) to our total amount. Of course, with simple interest our earnings are based on our original amount, not the “new total”. Connecting the dots gives us a trendline: we’re following a path of $50/year. Our payouts look like a staircase because we’re only paid at the end of the year, but the trajectory still works.</p>

<p><strong>Simple interest keeps the same trajectory:</strong> we earn “P*r” each year, no matter what ($50/year in this case). That straight line perfectly predicts where we’ll end up. </p>

<p>The idea of “following a trajectory” may seem strange, but stick with it — it will really help when understanding the nature of e.</p>

<p>One point: the trajectory is “how fast” a bank account is growing at a certain moment. With simple interest, we’re stuck in a car going the same speed: $50/year, or 50 mph. In other cases, our rate may change, like a skydiver: they start off slow, but each second fall faster and faster. But at <strong>any instant</strong>, there’s a single speed, a single trajectory.</p>

<p>(The math gurus will call this trajectory a “derivative” or “gradient”. No need to hit a mosquito with the calculus sledgehammer just yet.)</p>

<h2>Basic Compound Interest</h2>

<p>Simple interest should make you squirm. <strong>Why can’t our interest earn money?</strong> We should use the bond payouts ($50/year) to buy more bonds. Heck, we should use the golden eggs to fund research into cloning golden geese!</p>

<p><strong>Compound growth means your interest earns interest</strong>. Einstein called it “one of the most powerful forces in nature”, and it’s true. When you have a growing thing, which creates more growing things, which creates more growing things… your return adds up fast.</p>

<p>The most basic type is period-over-period return, which usually means “year over year”. Reinvesting our interest annually looks like this:</p>

<p><img src="http://betterexplained.com/wp-content/uploads/interest/compound_interest.png" alt="compound interest graph" title="compound interest graph" width="500" height="286" border="0"></p>

<p>We earn $50 from year 0 - 1, just like with simple interest. But in year 1-2, now that our total is $150, we can earn $75 this year (50% * 150) giving us $225. In year 2-3 we have $225, so we earn 50% of that, or $112.50.</p>

<p>In general, we have (1 + r) times more “stuff” each year. After n years, this becomes:</p>

<p><img src="http://betterexplained.com/latexrender/pictures/84eda4d0ef8c277f6f5bc1dd0d60aca3.gif" title="\displaystyle{return = P \cdot (1 + r)^n}" alt="\displaystyle{return = P \cdot (1 + r)^n}"></p>

<p>Exponential growth outpaces simple, linear interest, which only had $250 in year 3 (100 + 3*50). Compound growth is useful when:</p>


<ul>
<li><strong>Interest can be reinvested</strong>, which is the case for most savings accounts.</li>
<li><strong>You want to predict a future value based on a growth trend</strong>. Most trends, like inflation, <span>GDP </span>growth, etc. are assumed to be “compoundable”. Yearly <span>GDP </span>growth of 3% over 10 years is really (1.03)^10 = 1.344, or a 34.4% increase over that decade.</li>
</ul>



<h2>Interest as a Factory</h2>

<p>The typical interpretation sees money as a “blob” that grows over time. This view works, but sometimes I like to see interest earnings as a “factory” that generates more money:</p>

<p><img src="http://betterexplained.com/wp-content/uploads/interest/compound_interest_factory.png" alt="compound interest factory" title="compound interest factory" width="500" height="286" border="0"></p>

<p>Here’s what’s happening:</p>


<ul>
<li><strong>Year 0:</strong> We start with $100.</li>
<li><strong>Year 1:</strong> Our $100 creates a $50 “bond”.</li>
<li><strong>Year 2:</strong> The $100 generates another $50 bond. The $50 generates a $25 bond. The total is 50 + 25 = 75, which matches up.</li>
<li><strong>Year 3:</strong> Things get a bit crazy. The $100 creates a third $50 bond. The two existing $50 bonds make $25 each. And the $25 makes a 12.50.</li>
<li><strong>Years 4 to infinity:</strong> Left as an exercise for the reader. (Don’t you love that textbook cop out?)</li>
</ul>



<p>This is an interesting viewpoint. The $100 just mindlessly cranks out $50 “factories”, which start earning money independently (notice the 3 blue arrows from the blue principal to the green $50s). These $50 factories create $25 factories, and so on.</p>

<p>The pattern seems complex, but it’s simpler in a way as well. The $100 has no idea what those zany $50s are up to: as far as the $100 knows, we’re only making $50/year.</p>

<p>So why’s this viewpoint useful?</p>


<ul>
<li><strong>You can separate the impact of the parent ($100) from the children.</strong> For example, at Year 3 we have $328 total. The parent has earned $150 (”3 * 50% * $100 = $150″, using the simple interest formula!). This means the “children” have contributed $328 - $150 - $100 = $128, or about 1/3 the total value.</li>
<li><strong>Breaking earnings into components helps understand e.</strong> Knowing more about e is a good thing because it shows up everywhere.</li>
</ul>



<p>And besides, seeing old ideas in a new light is always fun. For one of us, at least.</p>

<h2>Understanding the Trajectory</h2>

<p>Oh, we’re not done yet. One more insight — take a look at our trajectory:</p>

<p><img src="http://betterexplained.com/wp-content/uploads/interest/compound_intererest_traj.png" alt="compound interest trajectory" title="compound interest trajectory" width="500" height="286" border="0"></p>

<p>With simple interest, we kept the same pace forever ($50/year — pretty boring). With annually compounded interest, <strong>we get a new trajectory each year</strong>.</p>

<p>We deposit our money, go to sleep, and wake up at the end of the year:</p>


<ul>
<li><strong>Year 1:</strong> “Hey, waittaminute. I’ve got $150 bucks! I should be making $75/year, not $50!”. You yell at your banker, crank up the dial to $75/year, and go to sleep again.</li>
<li><strong>Year 2:</strong> “Hey! I’ve got $225, and should be making $112.50 per year!”. You scream at your bank and get the rate adjusted.</li>
</ul>



<p>This process repeats forever — we seem to never learn.</p>

<h2>Compound Interest Revisited</h2>

<p>Why are we waiting so long? Sure, waiting a year at a time is better than waiting “forever” (like simple interest), but I think we can do better. Let’s zoom in on a year:</p>

<p><img src="http://betterexplained.com/wp-content/uploads/interest/interest_gap.png" alt="interest gap" title="interest gap" width="500" height="291" border="0"></p>

<p>Look at what’s happening. The green line represents our starting pace ($50/year), and the solid area shows the cash in our account. After 6 months, we’ve earned $25 but don’t see a dime! More importantly, after 6 months we have the same trajectory as when we started. The <strong>interest gap</strong> shows where we’ve earned interest, but stay on our original trajectory (based on the original principal). We’re losing out on what we should be making.</p>

<p>Imagine I took your money and returned it after 6 months. <em>“Well, ya see, I didn’t use it for a full year, so I don’t really owe you any interest. After all, interest is measured per year. Per yeeeeeaaaaar. Not per 6 months.”</em> You’d smile and send Bubba to break my legs.</p>

<p>Annual payouts are man-made artifacts, used to keep things simple. But in reality, money should be earned all the time. We can pay interest after 6 months to reduce the gap:</p>

<p><img src="http://betterexplained.com/wp-content/uploads/interest/compound_interest_twice.png" alt="compound interest twice" title="compound interest twice" width="500" height="291" border="0"></p>

<p>Here’s what happened:</p>


<ul>
<li>We start with $100 and a trajectory of $50/year, like normal</li>
<li>After 6 months we get $25, giving us $125</li>
<li>We head out using the new trajectory: 50% * $125 = $62.5/year</li>
<li>After 6 months we collect 62.5/year times .5 year = 31.25. We have 125 + 31.25 = 156.25.</li>
</ul>



<p>The key point is that our trajectory improved halfway through, and we earned 156.25, instead of the “expected” 150. Also, early payout gave us a smaller gap area (in white), since our $25 of interest was doing work for the second half (it contributed the extra 6.25, or $25 * 50% * .5 years).</p>

<p>For a 1 year, the impact of rate r compounded t times is:</p>

<p><img src="http://betterexplained.com/latexrender/pictures/cbdaba6330edea305a52ec780410c072.gif" title="\displaystyle{(1 + r/t)^t}" alt="\displaystyle{(1 + r/t)^t}"></p>

<p>In our case, we had (1 + 50%/2)^2. Repeating this for n years (multiplying n times) gives:</p>

<p><img src="http://betterexplained.com/latexrender/pictures/efd242c5f71576c58d04ef22e68971d8.gif" title="\displaystyle{return = P \cdot (1 + r/t)^{tn}}" alt="\displaystyle{return = P \cdot (1 + r/t)^{tn}}"></p>

<p><strong>Compound interest reduces the “dead space” where our interest isn’t earning interest</strong>. The more frequently we compound, the smaller the gap between earning interest and updating the trajectory.</p>

<h2>Continuous Growth</h2>

<p>Clearly we want money to “come online” as fast as possible. Continuous growth is compound interest on steroids: you shrink the gap into oblivion, by dividing the year into more and more time periods:</p>

<p><img src="http://betterexplained.com/wp-content/uploads/interest/continuous_growth.png" alt="continuous growth" title="continuous growth" width="500" height="291" border="0"></p>

<p>The net effect is to make use of interest as soon as it’s created. We wait a millisecond, find our new sum, and go off in the new trajectory. Except it’s not every millisecond: it’s every nanosecond, picosecond, femtosecond, and intervals I don’t know the name for. Continuous growth keeps the trajectory perfectly in sync with your current amount.</p>

<p><a href="http://betterexplained.com/articles/an-intuitive-guide-to-exponential-functions-e/">Read the article on e</a> for more details (e is a special number, like pi, and is roughly 2.718). If we have rate r and time t (in years), the result is:</p>

<p><img src="http://betterexplained.com/latexrender/pictures/f5bd65d5c6bd4f5bd093a215e5ef3747.gif" title="\displaystyle{return = P \cdot e^{rt}}" alt="\displaystyle{return = P \cdot e^{rt}}"></p>

<p>If you have a 50% <span>APR, </span>it would be an <span>APY </span>of e^(.50) = 64.9% if compounded continuously. That’s a pretty big difference! Notice that e takes care of the icky parts, like dividing by an infinite number of periods.</p>

<p>Why’s this useful?</p>


<ul>
<li><strong>Most natural phenomena grow continuously</strong>. As mentioned earlier, physical phenomena grows on its own schedule: radioactive material doesn’t wait for the Earth to go around the Sun before deciding to decay. Any physical equation that models change is going to use e<sup>rt</sup>.</li>
<li><b>e<sup>rt</sup> is the adjustable, one-size-fits-all exponential</b>. It sounds strange, but e can even model the jumpy, staircase-like growth we’ve seen with compound interest. We’ll get into this in a later article.</li>
</ul>



<p>Most interest discussions leave e out, as continuous interest is not often used in financial calculations. (Daily compounding, (1 + r/365)<sup>365</sup>, is generous enough for your bank account, thank you very much. But seriously, daily compounding is a pretty good approximation of continuous growth.) </p>

<p>The exponential e is the bridge from our jumpy “delayed” growth to the smooth changes of the natural world.</p>

<h2>A Few Examples</h2>

<p>Let’s try a few examples to make sure it’s sunk in. Remember: the <span>APR </span>is the rate they give you, the <span>APY </span>is what you actually earn (your true return).</p>


<ul>
<li><strong>Is a 4.5 <span>APY </span>better than a 4.4 <span>APR, </span>compounded quarterly?</strong> You need to compare <span>APY </span>to <span>APY.</span> 4.4% compounded quarterly is (1 + 4.4%/4)^4 = 4.47%, so the 4.5% <span>APY </span>is still better.</li>
<li><strong>Should I pay my mortgage at the end of the month, or the beginning?</strong> The beginning, for sure. This way you knock out a chunk of debt early, preventing that “debt factory” from earning interest for 30 days. Suppose your loan <span>APY </span>is 6% and your monthly payment is $2000. By paying at the start of the month, you’d save $2000 * 6% = $120/year, or $3600 throughout a 30-year mortgage. And a few grand is nothing to sneeze at.</li>
<li><strong>Should I use several small payments, or one large payment?</strong>. You want to pay debt off as early as possible. $500/week for 4 weeks is better than $2000 at the end of the month. Each payment stops a few weeks’ worth of interest. The math is a bit tricker, but think of it as 4 $500 investments, each getting different return. In a month, the first payment saves 3 week’s worth of interest: 500 * (1 + daily rate)<sup>21</sup>. The next saves 2 weeks: 500 * (1 + daily rate)<sup>14</sup>. The third saves a week 500 * (1 + daily rate)<sup>7</sup> and the last payment doesn’t save any interest. Regardless of the details, <strong>prepayment will save you money.</strong></li>
</ul>



<p><strong>The general principle:</strong> When investing, get interest paid early, so it can compound. When borrowing, pay debt early to <em>prevent</em> that interest from compounding.</p>

<h2>Onward and Upward</h2>

<p>This is a lot for one sitting, but I hope you’ve seen the big picture:</p>


<ul>
<li><strong>The interest rate (APR) is the “speed” at which money grows</strong>.</li>
<li><strong>Compounding lets you adjust your “speed” as you earn more interest</strong>. The <span>APR </span>is the initial speed; the <span>APY </span>is the actual change during the year.</li>
<li><strong>Man-made growth uses (1+r)^n</strong>, or some variant. We like our loans to line up with years.</li>
<li><strong>Nature uses e^{rt}</strong>. The universe doesn’t particularly care for our solar calendar.</li>
<li><strong>Interest rates are tricky.</strong> When in doubt, ask for the <span>APY </span>and pay debt early.</li>
</ul>



<p>Treating interest in this funky way (trajectories and factories) will help us understand some of e’s cooler properties, which come in handy for calculus. Also, try the <a href="http://betterexplained.com/articles/the-rule-of-72/">Rule of 72</a> for a quick way to compute the effect of interest rates mentally (that investment with 6% <span>APY </span>will double in 12 years). Happy math.</p>

<p> </p><img src="http://feeds.feedburner.com/~r/Betterexplained/~4/216296180" height="1" width="1"></div>
</div>

<div class="feed">
  <div class="title">58. <a href="http://feeds.feedburner.com/~r/Betterexplained/~3/226266657/" target="_blank">How to Develop a Sense of Scale</a></div>
  <div class="content"><p> A sense of scale helps us better understand the world, and convey ideas more effectively. What’s more impressive?</p>


<ul>
<li>Bill Gates has 56 billion dollars.</li>
<li>Bill Gates earned over <a href="http://instacalc.com/?d=SG93IG11Y2ggQmlsbCBHYXRlcyBoYXMgZWFybmVkIHNpbmNlIE1pY3Jvc29mdCBoYXMgYmVlbiBpbiBleGlzdGVuY2Uu&amp;%23038;c=NTYgYmlsbGlvbiBkb2xsYXJzIC8gMzMgeWVhcnMgaW4gZG9sbGFycyAvIG1pbnV0ZXw1NiBiaWxsaW9uIGRvbGxhcnMgLyAzMyB5ZWFycyBpbiBkb2xsYXJzIC8gZGF5fHx8fHw&amp;%23038;s=sssssss&amp;%23038;v=0.9">$3000 per minute [$50/second]</a> since Microsoft was created. Spending 5 seconds to pick $100 off the floor is literally not a good use of his time.</li>
</ul>



<p>If you’re like me, the second statement makes your jaw drop. 56 billion is just another large number, but $3000 <em>per minute</em> is something vivid and “imaginable”. Let’s check out a few ways to convey a sense of scale.</p>

<h2>Compare Side By Side By Side</h2>

<p>A common way to put things in perspective is to literally line them up, side by side. We’re visual creatures. We like to <strong>see</strong>, not imagine abstract numbers. To our brains, a million, billion, and trillion all seem like large, vague numbers.</p>

<p>Apple knows this. Many of its ads compare products to everyday objects, rather than touting the raw dimensions:</p>

<p><img src="http://betterexplained.com/wp-content/uploads/scale/apple_ads.png" alt="Apple ads relative size"></p>

<p>The Macbook Air fits into a manilla envelope. The ipod nano is as thick as a pencil. Certain cameras fit in a box of altoids. You know their size without busting out a ruler. Just yesterday, I got a haircut with the #5 clippers (”As wide as your finger”) and knew what it meant. The hairdresser didn’t have to say “.875 inches”.</p>

<p>It seems backwards that “casual” measurements like a pencil’s width can be more useful than a count of millimeters. But we’re not machines — our everyday experience is with pencils, not millimeters, and we can easily imagine how much room a pencil takes.</p>

<p>Here’s a few more examples of side-by-side comparison in action — notice how well they convey a sense of scale.</p>

<p><strong>Relative size of planets &amp; stars</strong>. A great example, much preferred to “Boys and girls, the Sun’s diameter is 1000x larger than the Earth’s”.</p>

<p><embed src="http://www.youtube.com/v/Tfs1t-2rrOM&amp;#038;rel=1" width="425" height="355" allowScriptAccess="never" wmode="transparent" type="application/x-shockwave-flash"></embed></p>

<p><b><a href="http://www.merzo.net/">Relative Dimensions of Fictional Ships &amp; Characters</a></b>. Fun and interesting: occupy a geek for hours by asking how many <span>TIE </span>fighters would be needed to take out the Starship Enterprise.</p>

<p><a href="http://www.merzo.net/"><br>
<img src="http://betterexplained.com/wp-content/uploads/scale/relative_ship_sizes.png" alt="Relative ship sizes"></a></p>

<p><b><a href="http://www.nikon.co.jp/main/eng/feelnikon/discovery/universcale/nano.swf">Interactive Sense of Scale Flash App</a></b>. A fantastic way to visualize the relative sizes of objects.</p>

<p><a href="http://www.nikon.co.jp/main/eng/feelnikon/discovery/universcale/nano.swf"><img src="http://betterexplained.com/wp-content/uploads/scale/sense_of_scale_flash_app.png" alt="Relative ship sizes"></a></p>

<p>And of course, the famous power of ten video:</p>

<p><embed src="http://www.dailymotion.com/swf/x2ing5" width="420" height="331" allowFullScreen="true" allowScriptAccess="never" wmode="transparent" type="application/x-shockwave-flash"></embed></p>

<h2>Rescale and Resize</h2>

<p>Instead of looking up at the “big numbers”, we can shrink them to our level. Imagine the average person makes 50k/year, and a rich guy makes 500k/year. What’s the difference? </p>

<p>Well, instead of visualizing having 10x your money, imagine that things cost 10 times less. A new laptop? That’ll be 150 bucks. A new porsche? Only 6,000 dollars. A really nice house? 50k. Yowza. Things are <strong>cheap</strong> when you’re rich.</p>

<p>To understand Bill Gates’ scale, don’t think of 50 billion dollars and 5 billion/year income — it’s just another large number. Try to imagine having things cost 100,000 times less (and 100,000 is a pretty large number).</p>

<p>A laptop would be a few pennies. A porsche would be about 60 cents. Your $50M mansion would be a mere 500 bucks. You could “splurge”, spend $1000, and get everything you’ve ever needed. And you’re still earning 50k/year.</p>

<p>It’s much more vivid than “50 billion in the bank”, eh?</p>

<h2>Use What We Know: Time and Distance</h2>

<p>Sometimes, a different type of scale may be useful. We know time and distance, which cover a surprisingly broad range of sizes.</p>

<p>For most of us (myself included), millions, billions and trillions are “big”. It’s not intuitively obvious that a trillion is actually a million squared — that is, a trillion makes 1 million look imperceptible.</p>

<p>Check out these <a href="http://instacalc.com/?d=&amp;%23038;c=MSBtaWxsaW9uIHNlY29uZHMgaW4gZGF5c3wxIGJpbGxpb24gc2Vjb25kcyBpbiB5ZWFyc3wxIHRyaWxsaW9uIHNlY29uZHMgaW4geWVhcnN8fHx8&amp;%23038;s=sssssss&amp;%23038;v=0.9">brain-bending figures</a>:</p>


<ul>
<li>1 second is 1 second</li>
<li>1 million seconds is <strong>12 days</strong> (Interesting)</li>
<li>1 billion seconds is <strong>30 years</strong> (Wow, that’s a lot)</li>
<li>1 trillion seconds is <strong>30,000 years</strong> (Jumpin’ Jillikers!)</li>
</ul>



<p>Yowza. Do you feel the staggering difference between a trillion and a million? Between a billion and a million?</p>

<p>We get a <a href="http://instacalc.com/?d=&amp;%23038;c=MSBtaWxsaW9uIG1tIGluIGttfDEgYmlsbGlvbiBtbSBpbiBrbXwxIHRyaWxsaW9uIG1tIGluIGttfHx8fA&amp;%23038;s=sssssss&amp;%23038;v=0.9">similiar effect when thinking about distance</a>:</p>


<ul>
<li>1 millimiter is 1 mm (pretty tiny)</li>
<li>1 million mm is a kilometer (down the street)</li>
<li>1 billion mm is a 1000 km (600 miles — partway across the country)</li>
<li>1 trillion mm is 1,000,000 km (Going around the world 25 times, almost as wide as the Sun)</li>
</ul>



<p>Again, see the difference? How small a million is (”down the street”) compared to the size of the Sun?</p>

<p>These numbers come in handy in many applications:</p>


<ul>
<li>99.999% reliability (”Five 9’s”) means an error rate of 10 out of a million. That is, you can be offline for only 10 seconds every 12 days. Or, you can have a tolerance of 10mm for every kilometer. That’s pretty accurate!</li>
<li>“One part per million” is often used by chemists to measure concentrations of substances. One ppm is like having a presence of 1 second in 12 days. And a part per trillion? You got it: 1 second every 30,000 years. That’s tiny.</li>
</ul>



<p>This apprach helped me understand how utterly gigantic a trillion is, and how precise 99.999% really is.</p>

<h2>Use People, Places and Things</h2>

<p>Yet another approach is to combine things we’re familiar with. Here’s a few numbers:</p>


<ul>
<li>There’s about 6.5 billion people on Earth</li>
<li>The internet has many billions of pages (call it a trillion to be safe)</li>
</ul>



<p>The US deficit of 10 trillion dollars would require a tax of $10 for every page on the internet to pay off (Yowza! And these are with generous estimates of the internet’s size). </p>

<p>A <a href="http://betterexplained.com/articles/the-quick-guide-to-guids/"><span>GUID</span></a>, or large ID number used in programming, is at no risk of running out. How many are there? Well, we could give everyone a copy of the internet, every second, for a billion years… and still have enough <span>GUID</span>s to identify each page. See how much bigger that is than “2^128″? (For the geeks: yes, the <a href="http://betterexplained.com/articles/understanding-the-birthday-paradox/">birthday paradox</a> makes the chance of collision much higher).</p>

<p>Seeing a number impact the real world (i.e. being applied to every page of the internet) makes an idea come to life.</p>

<h2>Summary</h2>

<p>This article isn’t really about numbers. It’s about understanding and communication, how we think and convey ideas. Do you insist on rigid scientific terms, or do you reach out to your audience with terms they understand? Do you think a “lay person” (someone who happened to choose a different field of study than you) is more interested in raw numbers, or side-by-side demonstrations?</p>

<p><strong>Developing a sense of scale helps us better understand the world and better convey that understanding.</strong></p>

<p>In a perfect universe, we’d hear “one trillion”, imagine a million by million grid, and say “wow”. But that’s not the case — in order to say “Wow!” we need (or at least I need) to imagine the number of seconds in 30,000 years, longer than modern human civilization.</p>

<p>When presenting ideas, remember that analogies can be more powerful, interesting and effective than a 1 with 12 zeros. </p><img src="http://feeds.feedburner.com/~r/Betterexplained/~4/226266657" height="1" width="1"></div>
</div>

<div class="feed">
  <div class="title">59. <a href="http://feeds.feedburner.com/~r/Betterexplained/~3/236189345/" target="_blank">Rescaling the Pythagorean Theorem</a></div>
  <div class="content"><p> The Pythagorean theorem can <a href="http://betterexplained.com/articles/surprising-uses-of-the-pythagorean-theorem/">apply to any shape</a>, not just triangles. It can measure <a href="http://betterexplained.com/articles/measure-any-distance-with-the-pythagorean-theorem/">nearly any type of distance</a>. And yet this 2000-year-old formula is still showing us new tricks.</p>

<p>Re-arranging the formula from this:</p>

<p><img src="http://betterexplained.com/latexrender/pictures/b38524e00b309a2bc7d18aa3815e3d80.gif" title="\displaystyle{c = \sqrt{a^2 + b^2}}" alt="\displaystyle{c = \sqrt{a^2 + b^2}}"></p>

<p>to this:</p>

<p><img src="http://betterexplained.com/latexrender/pictures/829ee48fe425a5b29d1c1d3eca8a4dd8.gif" title="\displaystyle{c= a \cdot \sqrt{1 + (b/a)^2}}" alt="\displaystyle{c= a \cdot \sqrt{1 + (b/a)^2}}"></p>

<p>helps us understand the relationship between slope (steepness) and distance. Let’s take a look.</p>

<h2>Rescale Your Triangle</h2>

<p>Scaling <a href="http://betterexplained.com/articles/how-to-develop-a-sense-of-scale/">leads to new insights</a>. Yes, $500k/year is a lot; but it really comes alive when you imagine things costing 10x less (A new laptop? $150. A new porsche? $6000).</p>

<p>Rescaling formulas can be eye-opening as well. Let’s start with our favorite 3-4-5 triangle and divide every side by 3:</p>

<p><center><br>
<img src="http://betterexplained.com/wp-content/uploads/pythagorean/triangle_rescale.png" alt="Triangle rescale" title="Triangle rescale">
</center></p>

<p>What happened?</p>

<p>Well, we have a smaller red triangle with sides 3/3 (aka 1), 4/3 and 5/3. We’ve got a mini version of the large triangle, and the Pythagorean Theorem still holds:</p>

<p><img src="http://betterexplained.com/latexrender/pictures/1ed969f6e65bedd35367b9776bd1471d.gif" title="\displaystyle{1^2 + (4/3)^2 = (5/3)^2}" alt="\displaystyle{1^2 + (4/3)^2 = (5/3)^2}"></p>

<h2>So Why’s This Special?</h2>

<p>It doesn’t seem like much, but there’s some surprising insights:</p>

<p><center><br>
<img src="http://betterexplained.com/wp-content/uploads/pythagorean/general_rescale.png" alt="General Triangle Rescaled" title="General Triangle Rescaled">
</center></p>

<p>First, we can <strong>rescale any triangle</strong> to have 1 as the smallest side (divide by “a”). All similar triangles (i.e. those with the same ratios, like 3-4-5 and 6-8-10) will shrink into the <strong>same mini triangle</strong>.</p>

<p>This mini triangle has an interesting property: it only cares about the ratio <strong>b/a</strong>. The only “meaningful” numbers are 1 and (b/a), giving:</p>

<p><img src="http://betterexplained.com/latexrender/pictures/a238bf524fdb34b17c65b522d5e0458c.gif" title="\displaystyle{mini \hspace{4 pt} hypotenuse = \sqrt{1 + (b/a)^2}}" alt="\displaystyle{mini \hspace{4 pt} hypotenuse = \sqrt{1 + (b/a)^2}}"></p>

<p>And what’s special about b/a? It’s the slope of the hypotenuse line! It’s called the slope, the <a href="http://betterexplained.com/articles/vector-calculus-understanding-the-gradient/">gradient</a>, the derivative, rise over run — whatever the label, <strong>b/a is the rate at which the hypotenuse changes!</strong></p>

<p>This makes sense. For every unit traveled along the short leg, we gain “slope units (b/a)” on the other leg. In a 3-4-5 triangle, we go 4/3 units “North” for every 1 unit “East”. And the length of our hypotenuse increases 5/3 (1.66) for every 1 unit East.</p>

<p>The result is pretty cool: we used the steepness of the hypotenuse (b/a) to find the distance traveled per unit East ( sqrt(1 + (b/a)^2 ).</p>

<h2>An Example, Please</h2>

<p>This is a bit weird, so let’s do an example. Suppose we’ve gone 5 units East and 12 units North. What’s our distance from the starting point?</p>

<p>The traditional approach plugs in the Pythagorean Theorem to get c = sqrt(5<sup>2</sup> + 12<sup>2</sup>) = 13. It works, but let’s try our mini-triangle method:</p>

<p><center><br>
<img src="http://betterexplained.com/wp-content/uploads/pythagorean/5_12_example.png" alt="5 12 Triangle Rescaled" title="5 12 Triangle Rescaled">
</center></p>

<p>Instead of a large triangle with sides 5 and 12, scale down by 5: we get a mini triangle with sides 5/5 (or 1) and 12/5. The “mini hypotenuse” is then sqrt(1 + (12/5)^2) = 2.6. This means we travel 2.6 units along the hypotenuse for every 1 unit East. Going the full 5 units East (our original triangle) is 5 * 2.6 = 13 units. Neato — we got the same answer both ways.</p>

<p>But silly me, I made a mistake. Instead of 5 units on that trajectory, I meant 6. No 7. No wait, 8. 9, for sure.</p>

<p>Normally, we’d be furiously hammering that square root button to find the new distance. Maybe even using trigonometry to “make it easier”. But not today — since we’re on the same trajectory, we can re-use our scaling constant of 2.6:</p>

<p></p>

<p>We can find the new distance traveled with <strong>regular multiplication</strong>, with nary a square root in sight. Cool! This approach is faster for humans and computers alike — you wouldn’t believe the <a href="http://betterexplained.com/articles/understanding-quakes-fast-inverse-square-root/">crazy approaches</a> programmers take to avoid a square root.</p>

<h2>Static and Dynamic Formulas</h2>

<p>I’ve realized that our venerable Pythagorean Theorem focuses on <strong>a</strong> and <strong>b</strong> separately: </p>

<p><img src="http://betterexplained.com/latexrender/pictures/b38524e00b309a2bc7d18aa3815e3d80.gif" title="\displaystyle{c = \sqrt{a^2 + b^2}}" alt="\displaystyle{c = \sqrt{a^2 + b^2}}"></p>

<p>We consider a and b as separate elements, to be squared and summed. This approach is straightforward, and helps when designing bridges or making pictures of triangles. <strong>The traditional formula focuses on final values.</strong></p>

<p>But the rescaled version has a new twist:</p>

<p><img src="http://betterexplained.com/latexrender/pictures/e968da79fcac558efb64ed63101e5d8f.gif" title="\displaystyle{c = a \cdot \sqrt{ \left(1 + (b/a)^2 \right)} }" alt="\displaystyle{c = a \cdot \sqrt{ \left(1 + (b/a)^2 \right)} }"></p>

<p>We’re not that interested in the separate quantities — we want the <strong>ratio b/a</strong>, or the slope of the hypotenuse. This slope creates a scaling constant, sqrt(1 + (b/a)^2), that tells us how our “Eastward” motion translates to distance along our path. <strong>The dynamic formula focuses on rates of change.</strong></p>

<p>If we have a hypothetical function f(x), we might write the dynamic Pythagorean Theorem this way:</p>

<p><img src="http://betterexplained.com/latexrender/pictures/13d984169955652ea5b83c3b320199fd.gif" title="\displaystyle{distance \hspace{5pt} along \hspace{5pt} path = x \cdot \sqrt{1 + (slope)^2}}" alt="\displaystyle{distance \hspace{5pt} along \hspace{5pt} path = x \cdot \sqrt{1 + (slope)^2}}"></p>

<p>This concept is used in calculus to find the length of any line or curve — but we’ll save that for another day.</p>

<p>The key is to realize a single formula can be re-arranged and lead to new insights. Stay curious — we stop learning when we think we’ve “got it all figured out”.</p>

<h2>Appendix 1: Slope vs. Distance</h2>

<p>One point that confused me was separating the idea of slope (b/a) from distance traveled (the hypotenuse, c).</p>

<p><strong>Slope</strong> is b/a, rise over run — how much height you get when you increase width. How “steep” the hill is, so to speak. Unfortunately, the word “slope” makes us think of the side of the hill — but <strong>slope is really about height</strong>.</p>

<p><strong>Distance (the hypotenuse)</strong> is about the side of the hill — how far you’ve walked. The “steepness” isn’t that important — you’re laying a measuring tape on the ground, which could be flat, vertical or upside-down. Does the length of a board depend on how you hold it?</p>

<p>But, in our man-made world, <strong>slope and distance are related</strong> because we often express locations in terms of “units East (x coordinate)” and not “units along a path”. So when a map says “go 1 mile due East” and you’re in front a mountain (large slope), you end up traveling a large distance (more than 1 mile). When on a flat road (zero slope), 1 mile East is simply 1 mile East. The bigger the slope, the more distance you must travel to “go 1 mile East”.</p>

<p>Again, we see that the Pythagorean Theorem is not just about triangles — it can convert slope (steepness) into distance traveled. Happy math. </p><img src="http://feeds.feedburner.com/~r/Betterexplained/~4/236189345" height="1" width="1"></div>
</div>

<div class="feed">
  <div class="title">60. <a href="http://feeds.feedburner.com/~r/Betterexplained/~3/240957859/" target="_blank">How To Understand Combinations Using Multiplication</a></div>
  <div class="content"><p> Multiplication is a wonderful little operation. Depending on the context, it can </p>


<ul>
<li><a href="http://betterexplained.com/articles/rethinking-arithmetic-a-visual-guide/">Scale or stretch</a> a number</li>
<li><a href="http://betterexplained.com/articles/a-visual-intuitive-guide-to-imaginary-numbers/" title="complex multiplication">Rotate a vector</a>, without needing sine or cosine</li>
</ul>



<p>And today we’ll see yet another use: <strong>listing combinations</strong>.</p>

<p>Revisiting multiplication has a few uses:</p>


<ul>
<li><strong>It demystifies other parts of math</strong>. The binomial theorem, Boolean algebra (used in computer circuits) and even parts of calculus become easier with a new interpretation of “multiplication”.</li>
<li><strong>It keeps our brain fresh.</strong> <a href="http://betterexplained.com/articles/how-to-develop-a-mindset-for-math/">Math gives us models to work with</a>, and it’s good to see how one model can have many uses. Even a wrench can drive nails, once you understand the true nature of “being a hammer” (<i>very Zen, eh?</i>).</li>
</ul>



<p>The long multiplication you learned in elementary school is quite useful: we can find the possibilities of several coin flips, for example. Let’s take a look.</p>

<h2>You’ve Been Making Combinations All Along</h2>

<p>How would you find 12 × 34? It’s ok, you can do it on paper:</p>

<p><center><br>
<img src="http://betterexplained.com/wp-content/uploads/mult/basic_multiplication.png" alt="Basic multiplication" title="Basic multiplication">
</center></p>

<p><i>“Well, let’s see… 4 times 12 is 48. 3 times 12 is 36… but it’s shifted over one place, so it’s 360. Add 48 and 360 and you get… uh… carry the 1… 408. Phew.”</i></p>

<p>Not bad. But instead of doing 12 × 34 all at once, break it into steps:</p>

<p><img src="http://betterexplained.com/wp-content/uploads/mult/expanding_multiplication.png" alt="Expanding Multiplication" title="Expanding Multiplication"></p>

<p>What’s happening? Well, 4 × 12 is actually “4 x (10 + 2)” or “40 + 8″, right? We can view that first step (blue) as two separate multiplications (4×10 and 4×2). </p>

<p>We’re so used to combining and carrying that we merge the steps, but they’re there. (For example, 4 × 17 = 4 x (10 + 7) = 40 + 28 = 68, but we usually don’t separate it like that.)</p>

<p>Similarly, the red step of “3 × 12″ is really “30 × 12″ — the 3 is in the tens column, after all. We get “30 x (10 + 2)” or “300 + 60″. Again, we can split the number into two parts.</p>

<p>What does this have to do with combinations? Hang in there, you’ll see soon enough.</p>

<h2>Curses, Foiled Again</h2>

<p>Take a closer look at what happened: 12 × 34 is really (10 + 2) x (30 + 4) = 300 + 40 + 60 + 8. This breakdown looks suspiciously like algebra equation (a + b) * (c + d):</p>

<p><img src="http://betterexplained.com/wp-content/uploads/mult/listing_combinations.png" alt="Listing combinations" title="Listing combinations"></p>

<p>And yes, that’s what’s happening! In both cases we’re multiplying one “group” by another. We take each item in the red group (10 and 2) and combine it with every element of the blue group (30 and 4). We don’t mix red items with each other, and we don’t mix blue items with each other.</p>

<p>This combination technique is often called <span>FOIL </span>(first-inside-outside-last), and gives headaches to kids. But it’s not a magical operation! It’s just laying things out in a grid. <strong><span>FOIL </span>is already built into the way we multiply!</strong></p>

<p><img src="http://betterexplained.com/wp-content/uploads/mult/foil_multiplication.png" alt="Foil and multiplication" title="Foil and multiplication"></p>

<p>When doing long multiplication, we “know” we’re not supposed to multiply across: you don’t do 1 × 2, because they’re in the same row. Similarly, you don’t do a x b, because they’re in the same parenthesis. We only multiply “up and down” — that is, we need an item from the top row (1 or 2, a or b) and an item from the bottom row (3 or 4, c or d).</p>

<p>Everyday multiplication (aka <span>FOIL</span>) gives us a way to crank out combinations of two groups: one from group A, another from group B. And sometimes it’s nice having all the possibilities as an equation.</p>

<h2>Examples Make It Click</h2>

<p>Let’s try an example. Suppose we want to find every combination of flipping a coin twice. There’s a few ways to do it, like using a grid or decision tree:</p>

<p><img src="http://betterexplained.com/wp-content/uploads/mult/grid_and_tree.png" alt="Decision grid and tree" title="Decision grid and tree"></p>

<p>That’s fine, but let’s be different. We can turn the question into an <strong>equation</strong> using the following rules:</p>


<ul>
<li><strong>addition = OR</strong>. We can get heads OR tails: (h+t)</li>
<li><strong>multiplication = <span>AND</span></strong>. We have a first toss <span>AND </span>a second toss: (h+t) * (h+t)</li>
</ul>



<p>Wow! How does this work?</p>

<p>Well, we really just want to crank out combinations, just like doing (a+b) * (c+d) = ac + bc + ad + bd. Looking carefully, this format means we pick <strong>a</strong> OR <strong>b</strong>, and combine it with one of <strong>c</strong> OR <strong>d</strong>. </p>

<p>When we see an addition (a+b), we know it means we must choose one variable: this OR that. When we see a multiplication (group1 * group2), we know it means we take one item from each: this <span>AND </span>that. </p>

<p>The shortcuts “AND = multiply” &amp; “OR = add” are simply another way to describe the <strong>relationship</strong> inside the equation. (Be careful; when we say three hundred and four, most people think 304, which is right too. This <span>AND</span>/OR trick works in the context of <em>describing combinations</em>).</p>

<p>So, when all’s said and dune, we can turn the sentence “(heads OR tails) <span>AND </span>(heads OR tails)” into:</p>

<p><img src="http://betterexplained.com/latexrender/pictures/b6c5d0a26ee4a1bf0ceba38a12ab64e4.gif" title="\displaystyle{(h + t)\cdot(h + t)}" alt="\displaystyle{(h + t)\cdot(h + t)}"></p>

<p>And just for kicks, we can multiply it out:</p>

<p><img src="http://betterexplained.com/latexrender/pictures/dba1608d6c33e959433fdd0bc55592df.gif" title="\displaystyle{(h + t) \cdot (h + t) = h^2 + th + ht + t^2 = h^2 + 2ht + t^2}" alt="\displaystyle{(h + t) \cdot (h + t) = h^2 + th + ht + t^2 = h^2 + 2ht + t^2}"></p>

<p>The result “h<sup>2</sup> + 2ht + t<sup>2</sup>” shows us every possibility, just like the grids and decision trees. And the size (coefficient) of each combination shows the number of ways it can happen:</p>


<ul>
<li>h^2: There’s one way to get two heads (h<sup>2</sup> = hh = heads <span>AND </span>heads)</li>
<li>2ht: There’s two ways to get a head and tails (ht, th)</li>
<li>t^2: There’s one way to get two tails (tt)</li>
</ul>



<p>Neato. The sum of the coefficients is 1 + 2 + 1 = 4, the total number of possibilities. The chance of getting exactly one heads and one tails is 2/4 = 50%. We figured this out without a tree or grid — regular multiplication does the trick!</p>

<h2>Grids? Trees? I Figured That Out In My Head.</h2>

<p>Ok, hotshot, let’s expand the scope. How many ways can we get exactly 2 heads and 2 tails with 4 coin flips? What’s the chance of getting 3 or more heads?</p>

<p>Our sentence becomes: “(heads OR tails) <span>AND </span>(h OR t) <span>AND </span>(h OR t) <span>AND </span>(h OR t)”</p>

<p><img src="http://betterexplained.com/latexrender/pictures/76bade695a284ce8ccfcf514157708a1.gif" title="\displaystyle{(h+t)^4 = h^4 + 4h^3t + 6h^2t^2 + 4ht^3 + t^4}" alt="\displaystyle{(h+t)^4 = h^4 + 4h^3t + 6h^2t^2 + 4ht^3 + t^4}"></p>

<p>Looking at the result (it looks hard but <a href="http://en.wikipedia.org/wiki/Pascal’s_triangle" title="there are shortcuts">there are shortcuts</a>), there are <strong>6</strong> ways to get 2 heads and 2 tails. There’s 1 + 4 + 6 + 4 + 1 = 16 possibilities, so there’s only a 6/16 = 37.5% chance of having a “balanced” result after 4 coin flips. (It’s a bit surprising that it’s <i>much more likely</i> to be uneven than even).</p>

<p>And how many ways can we get 3 or more heads? Well, that means any components with h<sup>3</sup> or h<sup>4</sup>: 4 + 1 = 5. So we have 5/16 = 31.25% chance of 3 or more heads.</p>

<p>Sometimes equations are better than grids and trees — look at how much info we crammed into a single line! Formulas work great when you have a calculator or computer handy.</p>

<p>But most of all, we have another tool in our box: <strong>we can write possibilities as equations, and use multiplication to find combinations.</strong></p>

<h2>Where Next?</h2>

<p>There’s a few areas of math that benefit from seeing multiplication in this way:</p>


<ul>
<li><strong>Binomial Theorem</strong>. This scary-sounding theorem relates (h+t)^n to the coefficients. If you’re clever, you realize you can use <a href="http://betterexplained.com/articles/easy-permutations-and-combinations/">combinations and permutations</a> to figure out the exponents rather than having to multiply out the whole equation. This is what the binomial theorem does. We’ll cover more later — this theorem shows up in a lot of places, including calculus.</li>
</ul>




<ul>
<li><strong>Boolean Algebra</strong>. Computer geeks love converting conditions like OR and <span>AND </span>into mathematical statements. This type of <span>AND</span>/OR logic is used when designing computer circuits, and expressing possibilities with equations (not diagrams) is very useful. The fancy name of this technique is Boolean Algebra, which we’ll save for a rainy day as well.</li>
</ul>




<ul>
<li><strong>Calculus</strong>. Calculus gets a double bonus from this interpretation. First, the binomial theorem makes working with equations like x^n much easier. Second, one view of calculus is an “expansion” of multiplication. Today we got practice thinking that multiplication means a lot more than “repeated addition”. (”12 × 34″ means 12 groups of 34, right?)</li>
</ul>




<ul>
<li><strong>More advanced combinations</strong>. Let’s say you have 3 guests (Alice, Bob, and Charlie) and they are bringing soda, ice cream, or yogurt. Someone knocks at the door — what are the possibilities? (a + b + c) * (s + i + y). The equation has it all there.</li>
</ul>



<p>So you can teach an old dog like multiplication new tricks after all. Well, the tricks have always been there — it’s like discovering Fido has been barking poetry in morse code all this time.</p>

<p>And come to think of it, maybe <i>we’re the animal</i> that learned a new trick. The poetry was there, staring us in the face and we just didn’t recognize it (12 × 34 is based on combinations!). I know I had some forehead-slapping moments after seeing how similar combinations and regular multiplication really were.</p>

<p>Happy math. </p><img src="http://feeds.feedburner.com/~r/Betterexplained/~4/240957859" height="1" width="1"></div>
</div>

<div class="feed">
  <div class="title">61. <a href="http://feeds.feedburner.com/~r/Betterexplained/~3/251527614/" target="_blank">How To Analyze Data Using the Average</a></div>
  <div class="content"><p> The <em>average</em> is a simple term with several meanings. The type of average to use depends on whether you’re adding, multiplying, grouping or dividing work among the items in your set.</p>

<p><strong>Quick quiz:</strong> You drove to work at 30 mph, and drove back at 60 mph. What was your average speed?</p>

<p>Hint: It’s not 45 mph, and it doesn’t matter how far your commute is. Read on to understand the many uses of this statistical tool.</p>

<p><img src="http://betterexplained.com/wp-content/uploads/average/average_diagram.png" alt="Examples of the average" title="Examples of the average"></p>

<h2>But what does it mean?</h2>

<p>Let’s step back a bit: what is the “average” all about?</p>

<p>To most of us, it’s “the number in the middle” or a number that is “balanced”. I’m a fan of taking <a href="http://betterexplained.com/articles/a-visual-intuitive-guide-to-imaginary-numbers/">multiple</a> <a href="http://betterexplained.com/articles/rethinking-arithmetic-a-visual-guide/">viewpoints</a>, so here’s another interpretation of the average:</p>

<p><strong>The average is the value that can replace every existing item, and have the same result.</strong> If I could throw away my data and replace it with one “average” value, what would it be?</p>

<p>One goal of the average is to understand a data set by getting a “representative” sample. But the calculation depends on how the items in the group interact. Let’s take a look.</p>

<h2>The Arithmetic Mean</h2>

<p>The arithmetic mean is the most common type of average:</p>

<p><img src="http://betterexplained.com/latexrender/pictures/d23e2d3b50f08f8e9bb1c933eb5ce144.gif" title="\displaystyle{average = \frac{sum}{number} }" alt="\displaystyle{average = \frac{sum}{number} }"></p>

<p><img src="http://betterexplained.com/wp-content/uploads/average/arithmetic_mean.png" alt="Arithmetic mean" title="arithmetic mean"></p>

<p>Let’s say you weigh 150 lbs, and are in an elevator with a 100lb kid and 350lb walrus. What’s the average weight?</p>

<p>The real question is “If you replaced this merry group with 3 identical people and want the same load in the elevator, what should each clone weigh?” </p>

<p>In this case, we’d swap in three people weighing 200 lbs each [(150 + 100 + 350)/3], and nobody would be the wiser.</p>

<p>Pros: </p>


<ul>
<li>It works well for lists that are simply combined (added) together.</li>
<li>Easy to calculate: just add and divide.</li>
<li>It’s intuitive — it’s the number “in the middle”, pulled up by large values and brought down by smaller ones.</li>
</ul>



<p>Cons:</p>


<ul>
<li>The average can be skewed by outliers — it doesn’t deal well with wildly varying samples. The average of 100, 200 and -300 is 0, which is misleading.</li>
</ul>



<p>The arithmetic mean works great 80% of the time; many quantities are added together. Unfortunately, there’s always those 20% of situations where the average doesn’t quite fit.</p>

<h2>Median</h2>

<p>The median is “the item in the middle”. But doesn’t the average (arithmetic mean) imply the same thing? What gives?</p>

<p>Humor me for a second: what’s the “middle” of these numbers?</p>


<ul>
<li>1, 2, 3, 4, 100</li>
</ul>



<p>Well, 3 is the middle of the list. And although the average (22) is somewhere in the “middle”, 22 doesn’t really represent the distribution. We’re more likely to get a number closer to 3 than to 22. The average has been pulled up by 100, an outlier.</p>

<p>The median solves this problem by taking the <strong>number in the middle of a sorted list</strong>. If there’s two middle numbers (even number of items), just take their average. Outliers like 100 only tug the median along one item in the sorted list, instead of making a drastic change: the median of 1 2 3 4 is 2.5.</p>

<p><img src="http://betterexplained.com/wp-content/uploads/average/median.png" alt="Arithmetic mean" title="arithmetic mean"></p>

<p>Pros:</p>


<ul>
<li>Handles outliers well — often the most accurate representation of a group</li>
<li>Splits data into two groups, each with the same number of items</li>
</ul>



<p>Cons:</p>


<ul>
<li>Can be harder to calculate: you need to sort the list first</li>
<li>Not as well-known; when you say “median”, people may think you mean “average”</li>
</ul>



<p>Some jokes run along the lines of “Half of all drivers are below average. Scary, isn’t it?”. But really, in your head, you know they should be saying “half of all drivers are below <em>median</em>“.</p>

<p>Figures like housing prices and incomes are often given in terms of the median, since we want an idea of <strong>the middle of the pack</strong>. Bill Gates earning a <a href="http://betterexplained.com/articles/how-to-develop-a-sense-of-scale/">few billion</a> extra one year might bump up the average income, but it isn’t relevant to how a regular person’s wage changed. We aren’t interested in “adding” incomes or house prices together — we just want to find the middle one.</p>

<p>Again, the type of average to use depends on how the data is used.</p>

<h2>Mode</h2>

<p>The mode sounds strange, but it just means <strong>take a vote</strong>. And sometimes a vote, not a calculation, is the best way to <strong>get a representative sample</strong> of what people want.</p>

<p>Let’s say you’re throwing a party and need to pick a day (1 is Monday and 7 is Sunday). The “best” day would be the option that satisfies the most people: an average may not make sense. (<i>“Bob likes Friday and Alice likes Sunday? Saturday it is!”</i>).</p>

<p>Similarly, colors, movie preferences and much more can be <a href="http://betterexplained.com/articles/measure-any-distance-with-the-pythagorean-theorem/">measured with numbers</a>. But again, the ideal choice may be the mode, not the average: the “average” color or “average” movie could be… unsatisfactory (Rambo meets Pride and Prejudice).</p>

<p><img src="http://betterexplained.com/wp-content/uploads/average/mode.png" alt="Mode" title="mode"></p>

<p>Pros:</p>


<ul>
<li>Works well for exclusive voting situations (this choice or that one; no compromise)</li>
<li>Gives a choice that the most people wanted (whereas the average can give a choice that nobody wanted).</li>
<li>Simple to understand</li>
</ul>



<p>Cons:</p>


<ul>
<li>Requires more effort to compute (have to tally up the votes)</li>
<li>“Winner takes all” — there’s no middle path</li>
</ul>



<p>The term “mode” isn’t that common, but now you know what button to look for when playing around with your favorite statistics program.</p>

<h2>Geometric Mean</h2>

<p>The “average item” depends on how we use our existing elements. Most of the time, items are added together and the arithmetic mean works fine. But sometimes we need to do more. When dealing with investments, area and volume, we don’t add factors, we multiply them.</p>

<p>Let’s try an example. Which portfolio do you prefer, i.e. which has a better <strong>typical year</strong>? </p>


<ul>
<li>Portfolio A: +10%, -10%, +10%, -10%</li>
<li>Portfolio B: +30%, -30%, +30%, -30%</li>
</ul>



<p>They look pretty similar. Our everyday average (arithmetic mean) tells us they’re both rollercoasters, but should average out to zero profit or loss. And maybe B is better because it seems to gain more in the good years. Right?</p>

<p><strong>Wrongo!</strong> Talk like that will get you burned on the <a href="http://betterexplained.com/articles/what-you-should-know-about-the-stock-market/">stock market</a>: investment returns are multiplied, not added! We can’t be all willy-nilly and use the arithmetic mean — we need to find the actual rate of return:</p>


<ul>
<li>Portfolio A:
<ul>
<li>Return: 1.1 * .9 * 1.1 * .9 = .98 (2% loss)</li>
<li>Year-over-year average: (.98)^(1/4) = 0.5% loss per year (this happens to be about 2%/4 because the numbers are small).</li>
</ul>
</li>
<li>Portfolio B:
<ul>
<li>1.3 * .7 * 1.3 * .7 = .83 (17% loss)</li>
<li>Year-over-year average: (.83)^(1/4) = 4.6% loss per year.</li>
</ul>
</li>
</ul>



<p>A 2% vs 17% loss? That’s a huge difference! I’d stay away from both portfolios, but would choose A if forced. We can’t just add and divide the returns — that’s not how <a href="http://betterexplained.com/articles/an-intuitive-guide-to-exponential-functions-e/">exponential growth</a> works.</p>

<p><img src="http://betterexplained.com/wp-content/uploads/average/geometric_mean.png" alt="Geometric Mean" title="Geometric Mean"></p>

<p>Some more examples:</p>


<ul>
<li><strong>Inflation rates:</strong> You have inflation of 1%, 2%, and 10%. What was the average inflation during that time? (1.01 * 1.02 * 1.10)^(1/3) = <a href="http://instacalc.com/?d=&amp;%23038;c=KDEuMDEgKiAxLjAyICogMS4xMCleKDEvMyl8fHx8fHw&amp;%23038;s=sssssss&amp;%23038;v=0.9">4.3%</a></li>
<li><strong>Coupons:</strong> You have coupons for 50%, 25% and 35% off. Assuming you can use them all, what’s the average discount? (i.e. What coupon could be used 3 times?). (.5 * .75 * .65)^(1/3) = <a href="http://instacalc.com/?d=&amp;%23038;c=KC41ICogLjc1ICogLjY1KV4oMS8zKXwxIC0gcjF8fHx8fA&amp;%23038;s=sssssss&amp;%23038;v=0.9">37.5%</a>. Think of coupons as a “negative” return — for the store, anyway <img src="http://betterexplained.com/wp-includes/images/smilies/icon_smile.gif" alt=":)"> .</li>
<li><strong>Area</strong>: You have a plot of land 40 × 60 yards. What’s the “average” side — i.e., how large would the corresponding square be? (40 * 60)^(0.5) = <a href="http://instacalc.com/?d=&amp;%23038;c=KDQwICogNjApXiguNSl8fHx8fHw&amp;%23038;s=sssssss&amp;%23038;v=0.9">49 yards</a>.</li>
<li><strong>Volume</strong>: You’ve got a shipping box 12 × 24 × 48 inches. What’s the “average” size, i.e. how large would the corresponding cube be? (12 * 24 * 48)^(1/3) = <a href="http://instacalc.com/?d=&amp;%23038;c=KDQwICogNjApXiguNSl8fHx8fHw&amp;%23038;s=sssssss&amp;%23038;v=0.9">24 inches</a>.</li>
</ul>



<p>I’m sure you can find many more examples: <strong>the geometric mean finds the “typical element” when items are multiplied together.</strong> I had wondered for a long time why the geometric mean was useful — now we know.</p>

<h2>Harmonic Mean</h2>

<p>The harmonic mean is more difficult to visualize, but is still useful. (By the way, “harmonics” refer to numbers like 1/2, 1/3 — 1 over anything, really.) The harmonic mean helps us calculate <strong>average rates</strong> when several items are working together. Let’s take a look.</p>

<p>If I have a rate of 30 mph, it means I get some result (going 30 miles) for every input (driving 1 hour). When averaging the impact of multiple rates (X &amp; Y), you need to think about outputs and inputs, not the raw numbers.</p>

<p><strong>average rate = total output/total input</strong></p>

<p><img src="http://betterexplained.com/wp-content/uploads/average/harmonic_mean.png" alt="harmonic mean" title="harmonic mean"></p>

<p>If we put both X and Y on a project, each doing the same amount of work, what is the average rate? Suppose X is 30 mph and Y is 60 mph. If we have them do similar tasks (drive a mile), the reasoning is:</p>


<ul>
<li>X takes 1/X time (1 mile = 1/30 hour)</li>
<li>Y takes 1/Y time (1 mile = 1/60 hour)</li>
</ul>



<p>Combining inputs and ouputs we get:</p>


<ul>
<li>Total output: 2 miles (X and Y each contribute “1″)</li>
<li>Total input: 1/X + 1/Y (each takes a different amount of time; imagine a relay race)</li>
</ul>



<p>And the average rate, output/input, is: </p>

<p><img src="http://betterexplained.com/latexrender/pictures/3f1b1fec8c06392c101df406fb826624.gif" title="\displaystyle{\frac{2}{ \frac{1}{X} + \frac{1}{Y} }}" alt="\displaystyle{\frac{2}{ \frac{1}{X} + \frac{1}{Y} }}"></p>

<p>If we had 3 items in the mix (X, Y and Z) the average rate would be:</p>

<p><img src="http://betterexplained.com/latexrender/pictures/72b06d73cbda35e99c7f2b06702562ba.gif" title="\displaystyle{\frac{3}{ \frac{1}{X} + \frac{1}{Y} + \frac{1}{Z} }}" alt="\displaystyle{\frac{3}{ \frac{1}{X} + \frac{1}{Y} + \frac{1}{Z} }}"></p>

<p>It’s nice to have this shortcut instead of doing the algebra each time — even finding the average of 5 rates isn’t so bad. With our example, we went to work at 30mph and came back at 60mph. To find the average speed, we just use the formula.</p>

<p>But don’t we need to know how far work is? Nope! No matter how long the route is, X and Y have the same output; that is, we go R miles at speed X, and another R miles at speed Y. The average speed is the same as going 1 mile at speed X and 1 mile at speed Y:</p>

<p><img src="http://betterexplained.com/latexrender/pictures/82fb9eddb7bfd2f761dff3b7f1dd2e81.gif" title="\displaystyle{\frac{2R}{\frac{R}{30} + \frac{R}{60}} = \frac{2}{\frac{1}{30} + \frac{1}{60}} = 40}" alt="\displaystyle{\frac{2R}{\frac{R}{30} + \frac{R}{60}} = \frac{2}{\frac{1}{30} + \frac{1}{60}} = 40}"></p>

<p>It makes sense for the average to be skewed towards the slower speed (closer to 30 than 60). After all, we spend twice as much time going 30mph than 60mph: if work is 60 miles away, it’s 2 hours there and 1 hour back.</p>

<p><strong>Key idea:</strong> The harmonic mean is used when two rates contribute to the same workload. Each rate is in a <strong>relay race</strong> and contributing the same amount to the output. For example, we’re doing a round trip to work and back. Half the result (distance traveled) is from the first rate (30mph), and the other half is from the second rate (60mph).</p>

<p><strong>The gotcha:</strong> Remember that the average is <strong>a single element that replaces every element</strong>. In our example, we drive 40mph on the way there (instead of 30) and drive 40 mph on the way back (instead of 60). It’s important to remember that we need to replace each “stage” with the average rate.</p>

<p>A few examples:</p>


<ul>
<li><strong>Data transmission:</strong> We’re sending data between a client and server. The client sends data at 10 gigabytes/dollar, and the server receives at 20 gigabytes/dollar. What’s the average cost? Well, we average 2 / (1/10 + 1/20) = 13.3 gigabytes/dollar <em>for each part</em>. That is, we could swap the client &amp; server for two machines that cost 13.3 gb/dollar. Because data is both sent and received (each part doing “half the job”), our true rate is 13.3 / 2 = 6.65 gb/dollar.</li>
</ul>




<ul>
<li><strong>Machine productivity</strong>: We’ve got a machine that needs to prep and finish parts. When prepping, it runs at 25 widgets/hour. When finishing, it runs at 10 widgets/hour. What’s the overall rate? Well, it averages 2 / (1/25 + 1/10) = 14.28 widgets/hour <em>for each stage</em>. That is, the existing times could be replaced with two phases running at 14.28 widgets/hour for the same effect. Since a part goes through both phases, the machine completes 14.28/2 = 7.14 widgets/hour.</li>
</ul>




<ul>
<li><strong>Buying stocks</strong>. Suppose you buy $1000 worth of stocks each month, no matter the price (dollar cost averaging). You pay $25/share in Jan, $30/share in Feb, and $35/share in March. What was the average price paid? It is 3 / (1/25 + 1/30 + 1/35) = $29.43 (since you bought more at the lower price, and less at the more expensive one). And you have $3000 / 29.43 = 101.94 shares. The “workload” is a bit abstract — it’s turning dollars into shares. Some months use more dollars to buy a share than others, and in this case a high rate is bad.</li>
</ul>



<p>Again, the harmonic mean helps measure <strong>rates working together on the same result</strong>.</p>

<h2>Yikes, that was tricky</h2>

<p>The harmonic mean <em>is</em> tricky: if you have <strong>separate</strong> machines running at 10 parts/hour and 20 parts/hour, then your average really is 15 parts/hour since each machine is independent and you are <strong>adding the capabilities</strong>. In that case, the arithmetic mean works just fine.</p>

<p>Sometimes it’s good to double-check to make sure the math works out. In the machine example, we claim to produce 7.14 widgets/hour. Ok, how long would it take to make 7.14 widgets?</p>


<ul>
<li>Prepping: 7.14 / 25 = .29 hours</li>
<li>Finishing: 7.14 / 10 = .71 hours</li>
</ul>



<p>And yes, .29 + .71 = 1, so the numbers work out: it does take 1 hour to make 7.14 widgets. When in doubt, try running a few examples to make sure your average rate really is what you calculated.</p>

<h2>Conclusion</h2>

<p>Even a simple idea like the average has many uses — there are more uses we haven’t covered (center of gravity, weighted averages, expected value). The key point is this:</p>


<ul>
<li>The “average item” can be seen as the item that could replace all the others</li>
<li>The type of average depends on how existing items are used (Added? Multiplied? Used as rates? Used as exclusive choices?)</li>
</ul>



<p>It surprised me how useful and varied the different types of averages were for analyzing data. Happy math. </p><img src="http://feeds.feedburner.com/~r/Betterexplained/~4/251527614" height="1" width="1"></div>
</div>

<div class="feed">
  <div class="title">62. <a href="http://feeds.feedburner.com/~r/Betterexplained/~3/271625992/" target="_blank">How To Make a Bookmarklet For Your Web Application</a></div>
  <div class="content"><p> Browser buttons (bookmarklets) are shortcuts that act like a simple browser plugin. Their advantages include:</p>


<ul>
<li><strong>Fast installation</strong>: Just add a link to your bookmarks</li>
<li><strong>Convenient</strong>: Use features while on your current page</li>
<li><strong>Easy to write</strong>: Bookmarklets are just like making a webpage; there’s no need to write a whole browser plugin</li>
<li><strong>Cross-browser</strong>: The same bookmarklet can work in <span>IE,</span> Firefox, Opera and Safari. </li>
</ul>



<p>Here’s a few bookmarklets I use regularly:</p>


<ul>
<li><a href="http://code.jalenack.com/delicious/">Delicious buttons</a>: Tag a webpage (useful for browsers that don’t have the delicious toolbar, like Opera and Firefox 3)</li>
<li><a href="http://www.google.com/translate_tools?hl=en">Google translate</a>: Translate the current page in one click</li>
<li><a href="http://instacalc.com/blog/new-feature-instacalc-bookmarklet">InstaCalc Bookmarklet</a>: Open a real-time calculator on any site</li>
</ul>



<h2>How easy is it?</h2>

<p>Only one way to find out. Try the instacalc bookmarklet right here:</p>

<p> <a href="javascript:void(0);"><br>
<img src="http://instacalc.com/blog/wp-content/uploads/images/bookmarklet_full.png" style="border:0px"></a></p>


<ul>
<li>Click this link: <a href="javascript:void(0);" style="border:2px solid #999;padding:2px;background:#cacaca;font-weight:bold">instacalc bookmarklet</a>. </li>
<li>A calculator opens in the corner of the page. Type 1 + 1 to see the result.</li>
<li>Select this text <b><code>15 mph in fps</code></b> and click the link again. Voila! The text is automatically inserted.</li>
<li>Close the window by clicking the red “x”</li>
</ul>



<p>Neat, eh? No install, just click and go. To save the bookmarklet, right click the link and “add to favorites/bookmarks”. Now you can open the calculator on any page.</p>

<p>Today we’ll walk through the anatomy of a bookmarklet, dissect a few, and give you the tools to build your own.</p>

<h2>Bookmarklets 101</h2>

<p>Regular bookmarks (aka favorites) are just locations to visit, like “http://gmail.com”. <strong>Bookmarklets are javascript code</strong> that the browser runs on the current page, and they’re marked by “javascript:” instead of “http://”.</p>

<p>When clicking a bookmarklet, imagine the page author wrote <code>&lt;script&gt;bookmarklet code here&lt;/script&gt;</code> — it can do almost anything. There are a few restrictions:</p>


<ul>
<li>Restricted length: Most <span>URL</span>s have a limit around 2000 characters, which limits the amount of code you can run. There’s a way around this.</li>
<li>No spaces allowed: Some browsers choke on spaces in a <span>URL, </span>so yourcodelookslikethis. We have a trick for this too.</li>
</ul>



<p>A simple bookmarklet looks like this:</p>

<p><code>&lt;a href=&quot;javascript:alert(&#39;hi&#39;);&quot;&gt;my link&lt;/a&gt;</code></p>

<p>Click <a href="javascript:void(0);">this link</a> to see it in action. This example isn’t too wild, but the key is that <strong>bookmarklets let you run code on an existing page.</strong></p>

<h2>What do you want to do?</h2>

<p>Your bookmarklet should do something useful. Ideas include:</p>


<ul>
<li><strong>Transform the current page.</strong> Do find/replace, highlight certain words or images, change <span>CSS </span>styles…</li>
<li><strong>Open/overlay a new page</strong>. Open a new page or draw a window on the current one, like a sidebar</li>
<li><strong>Send data to another site</strong>. Post, share, or upload the current <span>URL </span>or selected text (like Google translate).</li>
<li>Look at the <a href="https://www.squarefree.com/bookmarklets/">bookmarklet</a> <a href="http://www.bookmarklets.com/tools/categor.html">directories</a> for more inspiration.</li>
</ul>



<p>People spend most of their time on other sites. Web application authors, think creatively: how can people use your service when away from your site?</p>

<h2>Javascript for Bookmarklets</h2>

<p>A bookmarklet can use any <a href="http://betterexplained.com/articles/the-single-page-javascript-overview/">javascript command</a>, but certain ones are helpful:</p>

<p><strong>Get current page title:</strong>  <code>document.title</code><br>
<strong>Get the current <span>URL</span></strong>: <code>location.href</code><br>
<strong>Get the currently selected text</strong>:</p>


<pre>
<code>
  // get the currently selected text
  var t;
  try {
    t= ((window.getSelection &amp;&amp; window.getSelection()) ||
(document.getSelection &amp;&amp; document.getSelection()) ||
(document.selection &amp;&amp; 
document.selection.createRange &amp;&amp; 
document.selection.createRange().text));
  }
  catch(e){ // access denied on https sites
    t = &quot;&quot;;
  }
</code>
</pre>



<p><strong>Make text url-safe:</strong> <code>encodeURIComponent(text)</code> (and corresponding <code>decodeURIComponent()</code>). The page title or <span>URL </span>may have invalid characters (spaces, slashes, etc.) so it’s a good habit to encode them before sending them over (spaces become %20, etc.).</p>

<h2>Dissecting the Delicious Bookmarklet</h2>

<p>Here’s the code for the delicious bookmarklet (spaces added for readability):</p>



<pre>
<code>
javascript:location.href='http://del.icio.us/post?v=4;
url='+encodeURIComponent(location.href)+';
title='+encodeURIComponent(document.title)
</code>
</pre>



<p>And here’s what’s happening:</p>


<ul>
<li>Change to a new <span>URL </span>(to post the item)</li>
<li>Specify query parameters for the current document’s url (location.href) and title (document.title)</li>
<li>Make the paramaters url-safe with encodeURIComponent</li>
</ul>



<p>Once you tag and save the post, delicious sends you to the original page. How do they know where? Because it was sent along in the original request!</p>

<h2>Bookmarklet Interface Ideas</h2>

<p><strong>Imagine this:</strong> Your users are browsing for cat photos (or the journals of the American Chemical Society, but <a href="http://www.alexa.com/data/details/traffic_details/mlb.com?site0=acs.org&amp;%23038;site1=http%3A%2F%2Ficanhascheezburger.com%2F&amp;%23038;y=r&amp;%23038;z=3&amp;%23038;h=300&amp;%23038;w=610&amp;%23038;u%5B%5D=acs.org&amp;%23038;u%5B%5D=http%3A%2F%2Ficanhascheezburger.com%2F&amp;%23038;x=2008-04-16T05%3A53%3A49.000Z&amp;%23038;check=www.alexa.com&amp;%23038;signature=ecukkPnUzH3K0NIAEy25JIBcSyk%3D&amp;%23038;range=3m&amp;%23038;size=Medium">probably lolcats</a>) when they click your killer Web 2.0 bookmarklet. What happens?</p>

<p><img src="http://betterexplained.com/wp-content/uploads/bookmarklet/bookmarklet_interface_ideas.png" alt="Bookmarklet interface ideas" title="Bookmarklet interface ideas" width="500" height="382" border="0"></p>

<p>Common techniques are:</p>


<ul>
<li><strong>Take the user to a new page.</strong> Hopefully, you can use some data from the current page, otherwise it’s a regular bookmark.</li>
<li><strong>Frame the current page</strong>, like Google translate or Stumbleupon. This is similar to the first technique, but your site displays the old page inside the window.</li>
<li><strong>Overlay a new interface.</strong> Use <span>CSS </span><a href="http://www.brunildo.org/test/CPop.html">absolute positioning</a> to make a window in a set place, or <a href="http://divinentd.com/experiments/emulating-position-fixed.html">fixed positioning</a> to have the window follow you as you scroll. Beware the <span>CSS </span>bugs.</li>
</ul>



<p>Overlaid windows are great, but won’t that be hard to cram into a single line?</p>

<h2>The Big Trick: Dynamic Javascript</h2>

<p>Direct javascript works fine if you just want to redirect the user to another page, like the delicious bookmarklet. The no spaces, 2000 character limit really hurts when you want a more complicated interface.</p>

<p>There’s a fix: Our bookmarklet becomes a <strong>stub</strong> to load another (regular) javascript file. Here’s the code (spaces added for readability):</p>



<pre>
<code>
javascript:(function(){
  _my_script=document.createElement('SCRIPT');
  _my_script.type='text/javascript';
  _my_script.src='http://mysite.com/script.js?';
  document.getElementsByTagName('head')[0].appendChild(_my_script);
})();
</code>
</pre>



<p>Here’s how it works:</p>


<ul>
<li>Define an anonymous function to download the script</li>
<li>Create a <code>script</code> element, type <code>text/javascript</code>. We can’t use <code>var _my_script</code> because of the spaces, so choose a unique name.</li>
<li>Set the src of the script to our real javascript file. This file could pull down more javascript also.</li>
<li>Add the <code>script</code> element to the current page</li>
</ul>



<p>And that’s it! Our bookmarklet can now load any javascript we please, without the annoying restrictions. An added bonus: see how many people are using your tool, and you we can change our script (fix bugs or add features) on the server.</p>

<h2>Dissecting the Instacalc Bookmarklet</h2>

<p>Here’s the steps I went through to make the instacalc bookmarklet</p>

<p><strong>Create a bookmarklet interface</strong></p>

<p>I made a <a href="http://instacalc.com/gadget/bookmarklet.html">trimmed-down page</a> designed for the bookmarklet. If you click the page it appears fullscreen, but it resizes to the parent container. I planned on hosting this page inside a smaller iframe.</p>

<p><strong>Create a stub bookmarklet</strong></p>

<p>Because I wanted to get the currently selected text and overlay an interface, I knew I couldn’t fit my javascript into 2000 characters. So I used the dynamic javascript technique above to get the <a href="http://instacalc.com/gadget/instacalc.bookmarklet.js">real javascript file</a>.</p>

<p><strong>Careful caching</strong></p>

<p>I didn’t want to cache the bookmarklet javascript in case I wanted to change its behavior (but I did cache <a href="http://www.ircache.net/cgi-bin/cacheability.py?query=http%3A%2F%2Finstacalc.com%2Fgadget%2Fbookmarklet.html&amp;%23038;descend=on">the other files</a>). I added a dummy query parameter using <code>Math.random()</code>, which forces the browser to download the file each time. Since the script is small, this wasn’t too much of an issue.</p>


<pre>
<code>
instacalc_script.src='http://instacalc.com/gadget/instacalc.bookmarklet.js?x='+(Math.random());
</code>
</pre>



<p><strong>Build the interface</strong></p>

<p><a href="http://instacalc.com/gadget/instacalc.bookmarklet.js">The script</a> to build the interface is pretty straightforward. There’s some helper functions for encoding (instacalc stores data using base64). The script gets the selected text, constructs the <span>URL </span>for the iframe, and loads it up. It generates the <span>CSS </span>to have a fixed window on the top right of the screen, and a button to hide the window.</p>

<p>As a slight trick, if the bookmarklet is run again on the same page, it just shows the existing window instead of creating a new iframe.</p>

<h2>Tips &amp; Tricks</h2>

<p>Keep this in mind when making your bookmarklet:</p>

<p><strong>Make it friendly</strong>. Don’t interrupt the user’s flow. Bring up the window on the same page, or a new page that closes. If you must redirect the user to their original page. </p>

<p>This is important: the user was nice enough to use your service, so put ‘em back where they were!</p>

<p><strong>Make it fast</strong>. After you’ve got it working, tweak your bookmarklet’s speed using the following techniques</p>


<ul>
<li><a href="http://betterexplained.com/articles/how-to-optimize-your-site-with-http-caching/">http caching</a></li>
<li><a href="http://betterexplained.com/articles/how-to-optimize-your-site-with-gzip-compression/">gzip compression</a></li>
<li><a href="http://betterexplained.com/articles/speed-up-your-javascript-load-time/">javascript optimization</a></li>
</ul>



<p><strong>Give people instructions</strong>. Bookmarklets aren’t that common, so help people understand your tool. A few instructions (”right click this link and add to bookmarks/favorites”) and a screenshot go a long way.</p>

<p><strong>The gotcha: cross-domain communication</strong></p>

<p>Because of cross-domain security restrictions, your bookmarklets can’t use fancy-pants Ajax techniques to communicate with your site. The easiest way to communicate is through query parameters in a <span>URL.</span></p>

<p><strong>Debugging</strong> </p>

<p>What’s programming without bugs? Use <a href="http://betterexplained.com/articles/how-to-debug-web-applications-with-firefox/">firefox to debug</a> your javascript and <span>CSS.</span> Instead of clicking a bookmarklet each time, just make a page that runs the javascript file directly: <code>&lt;script src=&quot;...&quot;&gt;</code>. This is what the bookmarklet does.</p>

<p>Once the dummy page is working, try your bookmarklet on other sites. You’d be surprised how other <span>CSS </span>rules can mess up your carefully positioned elements (remember, you’re running in the context of another site).</p>

<h2>Links &amp; Resources</h2>

<p>I’m sure you’ll come up with crazy ways to use your newfound toy. The main benefits are simple installation, compatibility, and being able to interact with the current page.</p>


<ul>
<li>There are <a href="http://ted.mielczarek.org/code/mozilla/bookmarklet.html">crunching tools</a> to make your javascript bookmarklet-friendly. But it’s nice to just dynamically load the real script and be done with it. </li>
</ul>





<ul>
<li>People have put the <a href="http://ejohn.org/blog/super-fast-delicious-bookmarklet/">delicious bookmarklet on steroids</a>, such as letting you type the tags in the url before hitting the button. </li>
</ul>




<ul>
<li>Taking this to the extreme, <a href="http://www.greasespot.net/">Greasemonkey</a> is a firefox plugin letting you run really powerful scripts. For example, there was a script to add a “delete” button to Gmail before it was available.</li>
</ul>




<p>Have fun. </p><img src="http://feeds.feedburner.com/~r/Betterexplained/~4/271625992" height="1" width="1"></div>
</div>

<div class="feed">
  <div class="title">63. <a href="http://feeds.feedburner.com/~r/Betterexplained/~3/282281552/" target="_blank">A Gentle Introduction To Learning Calculus</a></div>
  <div class="content"><p> I have a love/hate relationship with calculus: it demonstrates the beauty of math and the agony of math education.</p>

<p>Calculus relates topics in an elegant, brain-bending manner. My closest analogy is Darwin’s Theory of Evolution: once understood, you start seeing Nature in terms of survival. You understand why drugs lead to resistant germs (survival of the fittest). You know why sugar and fat taste sweet (encourage consumption of high-calorie foods in times of scarcity). It all fits together.</p>

<p>Calculus is similarly enlightening. Don’t these formulas seem related in some way?</p>

<p><img src="http://betterexplained.com/wp-content/uploads/calculus/circle_sphere_formula.png" alt="circle sphere formula" title="circle and sphere formulas"></p>

<p>They are. But most of us learn these formulas independently. Calculus lets us start with “circumference = 2 * pi * r” and figure out the others — the Greeks would have appreciated this.</p>

<p><strong>Unfortunately, calculus can epitomize what’s wrong with math education</strong>. Most lessons feature contrived examples, arcane proofs, memorization and abstract symbol manipulation that body slam our intuition &amp; enthusiasm before they can put on their gloves.</p>

<p>It really shouldn’t be this way.</p>

<h2>Math, art, and ideas</h2>

<p>I’ve learned something from school: <strong>Math isn’t the hard part of math; motivation is.</strong> Specifically, staying encouraged despite</p>


<ul>
<li>Teachers focused more on publishing/perishing than teaching</li>
<li>Self-fulfilling prophecies that math is difficult, boring, unpopular or “not your subject”</li>
<li>Textbooks and curriculums more concerned with <a href="http://www.redshift.com/~jmichael/html/feynman.html">profits</a> and test results than insight</li>
</ul>



<p><a href="http://www.maa.org/devlin/LockhartsLament.pdf">‘A Mathematician’s Lament’ [pdf]</a> is an excellent essay on this issue that <a href="http://reddit.com/info/6baz9/comments/">resonated</a> <a href="http://www.metafilter.com/70699/A-Mathematicians-Lament">with</a> <a href="http://www.math.princeton.edu/~bbukh/natural.html">many people</a>:</p>

<blockquote>
“…if I had to design a mechanism for the express purpose of destroying a child’s natural curiosity and love of pattern-making, I couldn’t possibly do as good a job as is currently being done — I simply wouldn’t have the imagination to come up with the kind of senseless, soul-crushing ideas that constitute contemporary mathematics education.”<br>
</blockquote>

<p>Imagine teaching art like this: <strong>Kids, no fingerpainting in kindergarten.</strong> Instead, let’s study paint chemistry, the physics of light, and the anatomy of the eye. After 12 years of this, if the kids (now teenagers) don’t hate art already, they may begin to start coloring on their own. After all, they have the “rigorous, testable” fundamentals to start appreciating art. Right?</p>

<p>Poetry is similar. Imagine studying this quote (formula):</p>

<blockquote>
“This above all else: to thine own self be true, and it must follow, as night follows day, thou canst not then be false to any man.”<br>
 –William Shakespeare, Hamlet<br>
</blockquote>

<p>It’s an elegant way of saying “be yourself” (and if that means writing irreverently about math, so be it). But if this were math class, we’d be counting the syllables, analyzing the iambic pentameter, and mapping out the subject, verb and object.</p>

<p><strong>Math and poetry are fingers pointing at the moon. Don’t confuse the finger for the moon.</strong> Formulas are a <em>means to an end</em>, a way to express a mathematical truth.</p>

<p>We’ve forgotten that math is about ideas, not robotically manipulating the formulas that express them. </p>

<h2>Ok bub, what’s your great idea?</h2>

<p>Feisty, are we? Well, here’s what I won’t do: recreate the existing textbooks. If you need answers <em>right away</em> for that big test, there’s plenty of <a href="http://www.sosmath.com/calculus/calculus.html">websites</a>, <a href="http://ocw.mit.edu/OcwWeb/Mathematics/index.htm">class videos</a> and <a href="http://youtube.com/watch?v=EX_is9LzFSY">20-minute sprints</a> to help you out.</p>

<p><strong>Instead, let’s share the core insights of calculus</strong>. Equations aren’t enough — I want the “aha!” moments that make everything click.</p>

<p>Formal mathematical language is one just one way to communicate. Diagrams, animations, and just plain talkin’ can often provide more insight than a page full of proofs.</p>

<h2>But calculus is hard!</h2>

<p>I think anyone can appreciate the core ideas of calculus. We don’t need to be writers to enjoy Shakespeare.</p>

<p>It’s within your reach if you know algebra and have a general interest in math. Not long ago, reading and writing were the work of trained scribes. Yet today that can be handled by a 10-year old. Why?</p>

<p>Because we expect it. Expectations play a huge part in what’s possible. So <em>expect</em> that calculus is just another subject. Some people get into the nitty-gritty (the writers/mathematicians). But the rest of us can still admire what’s happening, and expand our brain along the way.</p>

<p>It’s about how far you want to go. I’d love for everyone to understand the core concepts of calculus and say “whoa”.</p>

<h2>So what’s calculus about?</h2>

<p>Some <a href="http://www.answers.com/calculus">define calculus</a> as “the branch of mathematics that deals with limits and the differentiation and integration of functions of one or more variables”. It’s correct, but not helpful for beginners.</p>

<p>Here’s my take: Calculus does to algebra what algebra did to arithmetic.</p>


<ul>
<li><strong>Arithmetic</strong> is about manipulating numbers (addition, multiplication, etc.).</li>
</ul>




<ul>
<li><strong>Algebra finds patterns between numbers</strong>: a<sup>2</sup> + b<sup>2</sup> = c<sup>2</sup> is a famous relationship, describing the sides of a right triangle. Algebra finds entire sets of numbers — if you know a and b, you can find c.</li>
</ul>




<ul>
<li><strong>Calculus finds patterns between equations</strong>: you can see how one equation (circumference = 2 * pi * r) relates to a similar one (area = pi * r<sup>2</sup> ).</li>
</ul>



<p>Using calculus, we can ask all sorts of questions:</p>


<ul>
<li>How does an equation grow and shrink? Accumulate over time?</li>
<li>When does it reach its highest/lowest point?</li>
<li>How do we use variables that are constantly changing? (Heat, motion, populations, …).</li>
<li>And much, much more!</li>
</ul>



<p>Algebra &amp; calculus are a problem-solving duo: calculus finds new equations, and algebra solves them. <strong>Like evolution, calculus expands your understanding of how Nature works.</strong></p>

<h2>An Example, Please</h2>

<p>Let’s walk the walk. Suppose we know the equation for circumference (2*pi*r) and want to find area. What to do?</p>

<p><strong>Realize that a filled-in disc is like a set of Russian dolls.</strong></p>

<p><img src="http://betterexplained.com/wp-content/uploads/calculus/disc_rings.png" alt="Disc and Rings" title="Disc and Rings"></p>

<p>Here are two ways to draw a disc:</p>


<ul>
<li>Make a circle and fill it in</li>
<li>Draw a bunch of rings with a thick marker</li>
</ul>



<p>The amount of “space” (area) should be the same in each case, right? And how much space does a ring use?</p>

<p>Well, the very largest ring has radius “r” and a circumference 2 * pi * r. As the rings get smaller their circumference shrinks, but it keeps the pattern of 2 * pi * current radius. The final ring is more like a pinpoint, with no circumference at all.</p>

<p><img src="http://betterexplained.com/wp-content/uploads/calculus/disc_rings_area.png" alt="Disc and Ring Area" title="Disc and Ring Area"></p>

<p>Now here’s where things get funky. <strong>Let’s unroll those rings and line them up.</strong> What happens?</p>


<ul>
<li>We get a bunch of lines, making a jagged triangle. But if we take thinner rings, that triangle becomes less jagged (more on this in future articles). </li>
<li>One side has the smallest ring (0) and the other side has the largest ring (2 * pi * r)</li>
<li>We have rings going from radius 0 to up to “r”. For each possible radius (0 to r), we just place the unrolled ring at that location.</li>
<li>The total area of the “ring triangle” = 1/2 base * height = 1/2 * r * (2 * pi * r) = pi * r<sup>2</sup>, which is the formula for area!</li>
</ul>



<p>Yowza! The combined area of the rings = the area of the triangle = area of circle!</p>

<p>This was a quick example, but did you catch the key idea? We took a disc, split it up, and put the segments together in a different way. Calculus showed us that a disc and ring are intimately related: a disc is really just a bunch of rings.</p>

<p>This is a recurring theme in calculus: <strong>Big things are made from little things.</strong> And sometimes the little things are easier to work with.</p>

<h2>A note on examples</h2>

<p>Many calculus examples are based on physics. That’s great, but it can be hard to relate: honestly, how often do you know <em>the equation for velocity</em> for an object? Less than once a week, if that.</p>

<p>I prefer starting with physical, visual examples because it’s how our minds work. That ring/circle thing we made? You could build it out of several pipe cleaners, separate them, and straighten them into a crude triangle to see if the math really works. That’s just not happening with your velocity equation.</p>

<h2>A note on rigor (for the math geeks)</h2>

<p>I can feel the math pedants firing up their keyboards. Just a few words on “rigor”.</p>

<p>Did you know we don’t learn calculus the way Newton and Leibniz discovered it? They used intuitive ideas of “fluxions” and “infinitesimals” which were replaced with limits because <strong>“Sure, it works in practice. But does it work in theory?”</strong>.</p>

<p>We’ve created complex mechanical constructs to “rigorously” prove calculus, but have lost our intuition in the process.</p>

<p>We’re looking at the sweetness of sugar from the level of brain-chemistry, instead of recognizing it as Nature’s way of saying “This has lots of energy. Eat it.”</p>

<p>I don’t want to (and can’t) teach an analysis course or train researchers. Would it be so bad if everyone understood calculus to the “non-rigorous” level that Newton did? That it changed how they saw the world, as it did for him?</p>

<p>A premature focus on rigor dissuades students and makes math hard to learn. Case in point: e is technically defined by a limit, but the <a href="http://betterexplained.com/articles/an-intuitive-guide-to-exponential-functions-e">intuition of growth</a> is how it was discovered. The natural log can be seen as an integral, or the <a href="http://betterexplained.com/articles/demystifying-the-natural-logarithm-ln/">time needed to grow</a>. Which explanations help beginners more?</p>

<p>Let’s fingerpaint a bit, and get into the chemistry along the way.</p>

<h2>Where next?</h2>

<p>My goal is to begin presenting a beautiful, oft-maligned subject in a new light. Many ideas are more intuitive than you think:</p>


<ul>
<li><a href="http://betterexplained.com/articles/how-to-analyze-data-using-the-average/">Averages</a></li>
<li><a href="http://betterexplained.com/articles/measure-any-distance-with-the-pythagorean-theorem/">Pythagorean Theorem</a></li>
<li><a href="http://betterexplained.com/articles/a-visual-intuitive-guide-to-imaginary-numbers/">Imaginary Numbers</a></li>
<li><a href="http://betterexplained.com/articles/divergence/">Div</a>, <a href="http://betterexplained.com/articles/vector-calculus-understanding-the-gradient/">Grad</a>, <a href="http://betterexplained.com/articles/flux/">Flux</a> and <a href="http://betterexplained.com/articles/vector-calculus-understanding-circulation-and-curl/">Curl</a> (if you already know vector calculus)</li>
</ul>



<p>My knowledge of calculus is still very mechanical, but I know this can change. As I explore this topic I’ll cover the insights that worked, hoping you’ll chime in with what has helped you. Here’s the first:</p>


<ul>
<li><a href="http://betterexplained.com/articles/prehistoric-calculus-discovering-pi/">Prehistoric Calculus: Discovering Pi</a></li>
</ul>



<p>Happy math.<br>
 </p><img src="http://feeds.feedburner.com/~r/Betterexplained/~4/282281552" height="1" width="1"></div>
</div>

<div class="feed">
  <div class="title">64. <a href="http://feeds.feedburner.com/~r/Betterexplained/~3/300629795/" target="_blank">Prehistoric Calculus: Discovering Pi</a></div>
  <div class="content"><p> Pi is mysterious. Sure, you “know” it’s about 3.14159 because you read it in some book. But what if you had no textbooks, no computers, and no <a href="http://betterexplained.com/articles/a-gentle-introduction-to-learning-calculus/">calculus</a> (egads!) —  just your brain and a piece of paper. Could you find pi?</p>

<p>Archimedes found pi to 99.9% accuracy 2000 years ago — without decimal points or even the number zero! Even better, he devised techniques that became the foundations of calculus. I wish I learned his discovery of pi in school — it helps us understand what makes calculus tick.</p>

<h2>How do we find pi?</h2>

<p>Pi is the circumference of a circle with diameter 1. How do we get that number?</p>


<ul>
<li>Say pi = 3 and call it a day.</li>
<li>Draw a circle with a steady hand, wrap it with string, and measure with your finest ruler.</li>
<li>Use door #3</li>
</ul>



<p>What’s behind door #3? Math!</p>

<h2>How did Archimedes do it?</h2>

<p>Archimedes didn’t know the circumference of a circle. But he didn’t fret, and started with what he <em>did</em> know: the perimeter of a square. (He actually used hexagons, but squares are easier to work with and draw, so let’s go with that, ok?).</p>

<p>We don’t know a circle’s circumference, but for kicks let’s draw it between two squares:</p>

<p><img src="http://betterexplained.com/wp-content/uploads/calculus/pi_square.png" alt="Pi square estimate" title="Pi square estimate"></p>

<p>Neat — it’s like a racetrack with inner and outer edges. Whatever the circumference is, it’s <em>somewhere</em> between the perimeters of the squares: more than the inside, less than the outside.</p>

<p>And since squares are, well, <em>square</em>, we find their perimeters easily:</p>


<ul>
<li>Outside square: side = 1, perimeter = 4</li>
<li>Inside square: side = sqrt(.5<sup>2</sup> + .5<sup>2</sup>) = .7 [Thanks, <a href="http://betterexplained.com/articles/surprising-uses-of-the-pythagorean-theorem/">Pythagoras</a>], perimeter = 4 * .7 = 2.8</li>
</ul>



<p>We may not know where pi is, but that critter is scurrying somewhere from 2.8 to 4. Let’s say it’s halfway between, or <strong>pi = 3.4</strong>.</p>

<h2>Squares drool, octagons rule</h2>

<p>We estimated pi = 3.4, but honestly we’d be better off with the ruler and string. What makes our guess so bad?</p>

<p><strong>Squares are clunky</strong>. They don’t match the circle well, and the gaps make for a loose, error-filled calculation. But, increasing the sides (using the mythical octagon, perhaps) might give us a tighter fit and a better guess (<a href="http://en.wikipedia.org/wiki/Image:Archimedes_pi.svg">image credit</a>):</p>

<p><img src="http://betterexplained.com/wp-content/uploads/calculus/pi_polygon.png" alt="Pi Polygon Estimate " title="Pi Polygon Estimate"></p>

<p>Cool! As we yank up the sides, we get closer to the shape of a circle. </p>

<p>So, what’s the perimeter of an octagon? I’m not sure if I <a href="http://mathworld.wolfram.com/Octagon.html">learned that formula</a>. While we’re at it, we could use a 16-side-a-gon and a 32-do-decker for better guesses. What are their perimeters again?</p>

<p>Crickey, those are tough questions. Luckily, Archimedes used <a href="http://personal.bgsu.edu/~carother/pi/Pi3a.html">creative trigonometry</a> to devise formulas for the perimeter of shape when you double the number of sides:</p>

<p><strong>Inside perimeter:</strong> One segment of the inside (such as the side of a square) is sin(x/2), where x the angle spanning a side. For example, one side of the inside square is sin(90/2) = sin(45) ~ .7. The full perimeter is then 4 * .7 = 2.8, as we had before.</p>

<p><strong>Outside perimeter:</strong> One segment of the outside is tan(x/2), where x is the angle spanning one side. So, one segment of the outside perimeter is tan(45) = 1, for a total perimeter of 4.</p>

<p>Neat — we have a simple formula! Adding more sides makes the angle smaller: </p>


<ul>
<li>Squares have an inside perimeter of 4 * sin(90/2).</li>
<li>Octogons have eight 45-degree angles, for an inside perimeter of 8 * sin(45/2). </li>
</ul>



<p>Try it out — a square (sides=4) has 91% accuracy, and with an octagon (sides=8) we jump to 98%!</p>

<p></p>

<p>But there’s a problem: Archimedes didn’t have a calculator with a “sin” button! Instead, he used trig identities to <a href="http://personal.bgsu.edu/~carother/pi/Pi3c.html">rewrite sin and tan</a> in terms of their previous values:</p>


<ul>
<li>New outside perimeter <img src="http://betterexplained.com/latexrender/pictures/b75637153c349d351be833138e2fd20b.gif" title="\displaystyle{newOut}" alt="\displaystyle{newOut}">= <img src="http://betterexplained.com/latexrender/pictures/38b906ba9720b6c61deb05677a34ffe5.gif" title="\displaystyle{\frac{2}{\frac{1}{Inside} + \frac{1}{Outside} }}" alt="\displaystyle{\frac{2}{\frac{1}{Inside} + \frac{1}{Outside} }}"> [harmonic mean] </li>
</ul>




<ul>
<li>New inside perimeter <img src="http://betterexplained.com/latexrender/pictures/303afa3406244d285d5126c1f34960e0.gif" title="\displaystyle{newIn}" alt="\displaystyle{newIn}"> = <img src="http://betterexplained.com/latexrender/pictures/b79d47023e22682a66385bc2c8f65aab.gif" title="\displaystyle{\sqrt{Inside \cdot newOut}}" alt="\displaystyle{\sqrt{Inside \cdot newOut}}"> [geometric mean]</li>
</ul>




<p>These formulas just use arithmetic — no trig required. Since we started with known numbers like sqrt(2) and 1, we can repeatedly apply this formula to increase the number of sides and get a better gues for pi.</p>

<p>By the way, those <a href="http://betterexplained.com/articles/how-to-analyze-data-using-the-average/">special means</a> show up in strange places, don’t they? I don’t have a nice <em>intuitive</em> grasp of the trig identities involved, so we’ll save that battle for another day.</p>

<h2>Cranking the formula</h2>

<p>Starting with 4 sides (a square), we make our way to a better pi (<a href="http://betterexplained.com/wp-content/uploads/calculus/CalculatingPi.xls">download the spreadsheet</a>):</p>

<p><img src="http://betterexplained.com/wp-content/uploads/calculus/pi_table.png" alt="" title=""></p>

<p>Every round, we double the sides (4, 8, 16, 32, 64) and shrink the range where pi could be hiding. Let’s assume pi is halfway between the inside and outside boundaries.</p>

<p>After 3 steps (32 sides) we already have <strong>99.9%</strong> accuracy. After 7 steps (512 sides) we have the lauded “five nines”. And after 17 steps, or half a million sides, <strong>our guess for pi is as accurate as Excel</strong>. Not a bad technique, Archimedes!</p>

<p>Unfortunately, decimals hadn’t been invented in 250 <span>BC, </span>let alone spreadsheets. So Archimedes had to slave away with these formulas using <em>fractions</em>. He began with hexagons (6 sides) and continued 12, 24, 48, 96 until he’d had enough (ever try to take a square root using fractions alone?). His final estimate for pi, using a shape with 96 sides, was:</p>

<p><img src="http://betterexplained.com/latexrender/pictures/d27014a4f9d58cfc4bfaefca7e93f8bf.gif" title="\displaystyle{3 \frac{10}{71} &amp;lt; \pi &amp;lt; 3 \frac{1}{7}}" alt="\displaystyle{3 \frac{10}{71} &amp;lt; \pi &amp;lt; 3 \frac{1}{7}}"></p>

<p>The midpoint puts pi at 3.14185, which is over 99.9% accurate. Not too shabby!</p>

<p>If you enjoy fractions, the mysteriously symmetrical 355/113 is an <strong>extremely accurate (99.99999%)</strong> estimate of pi and was the best humanity had for nearly a millennium.</p>

<p>Some people use 22/7 for pi, but now you can chuckle “Good grief, 22/7 is merely the upper bound found by Archimedes 2000 years ago!” while adjusting your monocle. There’s even <a href="http://www.ams.org/featurecolumn/archive/pi.html">better formulas</a> out there too.</p>

<h2>Where’s the Calculus?</h2>

<p>Archimedes wasn’t “doing calculus” but he laid the groundwork for its development: start with a crude model (square mimicking a circle) and refine it.</p>

<p>Calculus revolves around these themes:</p>


<ul>
<li><strong>We don’t know the answer, but we’ve got a guess.</strong> We had a guess for pi: somewhere between 2.8 and 4. Calculus has many concepts such as Taylor Series to build a guess with varying degrees of accuracy.</li>
<li><strong>Let’s make our guess better</strong>. Archimedes discovered that adding sides made a better estimate. There are numerical methods to refine a formula again and again. For example, computers can start with a rough guess for the <a href="http://betterexplained.com/articles/understanding-quakes-fast-inverse-square-root/">square root</a> and make it better (faster than finding the closest answer from the outset).</li>
<li><strong>You can run but not hide</strong>. We didn’t know exactly where pi was, but trapped it between two boundaries. As we tightened up the outside limits (pun intended), we knew pi was hiding somewhere inside. This is formally known as the <a href="http://en.wikipedia.org/wiki/Squeeze_theorem">Squeeze Theorem</a>.</li>
<li><strong>Pi is an unreachable ideal.</strong> Finding pi is a process that never ends. When we see <img src="http://betterexplained.com/latexrender/pictures/7151cab809c0aeeb28ec21bfb56a732e.gif" title="\displaystyle{\pi}" alt="\displaystyle{\pi}"> it really means “You want perfection? That’s nice — everyone wants something. Just start cranking away and stop when pi is good enough.”.</li>
</ul>



<p>I’ll say it again: <strong>Good enough is good enough</strong>. A shape with 96 sides was accurate enough for anything Archimedes needed to build.</p>

<p>The idea that “close counts” is weird — shouldn’t math be <em>precise</em>? Math is a <a href="http://betterexplained.com/articles/how-to-develop-a-mindset-for-math/">model to describe the world</a>. Our equations don’t need to be razor-sharp if the universe and our instruments are fuzzy.</p>

<h2>Life Lessons</h2>

<p>Even math can have life lessons hidden inside. <strong>Sometimes the best is the enemy of the good</strong>. Perfectionism (”I need the exact value of pi!”) can impede finding good, usable results.</p>

<p>Whether making estimates or writing software, perhaps you can <strong>start with a rough version and improve it over time</strong>, without fretting about the perfect model (it worked for Archimedes!). Most of the accuracy may come from the initial stages, and future refinements may be a lot of work for little gain (the <a href="http://betterexplained.com/articles/understanding-the-pareto-principle-the-8020-rule/">Pareto Principle</a> in action).</p>

<p>Ironically, the “crude” techniques seen here led to calculus, which in turn led to better formulas for pi.</p>

<h2>Math Lessons</h2>

<p>Calculus often lacks an intuitive grounding — we can count apples to test arithmetic, but it’s hard to think about abstract equations that are repeatedly refined.</p>

<p>Archimedes’ discovery of pi is a vivid, concrete example for our toolbox. Just like geometry refines our intuition about lines and angles, calculus defines the rules about equations that get better over time. Examples like this help use intuition as a starting point, instead of learning new ideas in a vacuum.</p>

<p>Later, we’ll discuss what it means for numbers to be “close enough”. Just remember that 96 sides was good enough for Archimedes, and half a million sides is good enough for Excel. We’ve all got our limits. </p><img src="http://feeds.feedburner.com/~r/Betterexplained/~4/300629795" height="1" width="1"></div>
</div>

<div class="feed">
  <div class="title">65. <a href="http://feeds.feedburner.com/~r/Betterexplained/~3/330839580/" target="_blank">Intuitive Guide to Angles, Degrees and Radians</a></div>
  <div class="content"><p> It’s an obvious fact that circles should have 360 degrees. Right?</p>

<p>Wrong. Most of us have <em>no idea</em> why there’s 360 degrees in a circle. We memorize a magic number as the “size of a circle” and set ourselves up for confusion when studying advanced math or physics, with their so called “radians”.</p>

<p>“Radians make math easier!” the <a href="http://en.wikipedia.org/wiki/Radian#Reasons_why_radians_are_preferred_in_mathematics">experts say</a>, without a simple reason why (discussions involving Taylor series are not simple). Today we’ll uncover what radians really are, and the intuitive reason they make math easier.</p>

<h2>Where do degrees come from?</h2>

<p>Before numbers and language we had the stars. Ancient civilizations used astronomy to mark the seasons, predict the future, and appease the gods (when making human sacrifices, <em>they’d better be on time</em>).</p>

<p>How is this relevant to angles? Well, bub, riddle me this: isn’t it strange that <strong>a circle has 360 degrees and a year has 365 days?</strong>. And isn’t it weird that constellations just happen to circle the sky during the course of a year?</p>

<p>Unlike a pirate, I bet you landlubbers can’t determine the seasons by the night sky. Here’s the <a href="http://en.wikipedia.org/wiki/Big_Dipper">Big Dipper</a> (Great Bear) as seen from New York City in 2008 (<a href="http://www.astroviewer.com/interactive-night-sky-map.php">try any city</a>):</p>

<p><img src="http://betterexplained.com/wp-content/uploads/angles/constellation_rotation.png" alt="constellation rotation" title="constellation rotation"></p>

<p>Constellations <a href="http://www.astro.uiuc.edu/~kaler/celsph.html">make a circle</a> throughout the year — ever see the Big Dipper upside down sometimes? (Never fear, it’ll be rightside-up in 6 months). Here’s <a href="http://en.wikipedia.org/wiki/Degree_(angle)#History">a theory</a> about how degrees came to pass:</p>


<ul>
<li>Humans noticed that constellations moved in a full circle every year</li>
<li>Every day, they moved a tiny bit (” a degree”)</li>
<li>Since a year has about 360 days, a circle had 360 degrees</li>
</ul>



<p>But, but… why not <em>365 degrees</em> in a circle?</p>

<p>Cut ‘em some slack: they had <em>sundials</em> and didn’t know a year should have a convenient 365.242199 degrees like you do.</p>

<p>360 is close enough for government work. It fits nicely into the Babylonian base-60 number system, and divides well (by 2, 3, 4, 6, 10, 12, 15, 30, 45, 90… you get the idea).</p>

<h2>Basing mathematics on the Sun seems perfectly reasonable</h2>

<p>Earth lucked out: ~360 is a great number of days to have in a year. But it does seem arbitrary: on Mars we’d have roughly ~680 degrees in a circle, for the longer Martian year (Martian days are longer too, but you get the idea). And in parts of Europe they’ve used <a href="http://en.wikipedia.org/wiki/Grad_(angle)">gradians</a>, where you divide a circle into 400 pieces.</p>

<p>Many explanations stop here saying, “Well, the degree is arbitrary but we need to pick <em>some</em> number.” Not here: we’ll see that <strong>the entire premise of the degree is backwards</strong>.</p>

<h2>Radians Rule, Degrees Drool</h2>

<p>A degree is the amount I, an observer, need to tilt my head to see you, the mover. It’s a tad self-centered, don’t you think?</p>

<p>Suppose you saw a friend go running on a large track:</p>

<blockquote>
“Hey Bill, how far did you go?”<br>
“Well, I had a really good pace, I think I went 6 or 7 mile–”<br>
“Shuddup. How far did I turn my head to see you move?”<br>
“What?”<br>
“I’ll use small words for you. Me in middle of track. You ran around. How…much…did…I…turn…my…head?”<br>
“Jerk.”<br>
</blockquote>

<p>Selfish, right? <strong>That’s how we do math!</strong> We write equations in terms of “Hey, how far did I turn my head see that planet/pendulum/wheel move?”. I bet you’ve never bothered to think about the pendulum’s feelings, hopes and dreams.</p>

<p>Do you think the equations of physics should be made simple for the mover or observer?</p>

<h2>Radians: The Unselfish Choice</h2>

<p>Much of physics (and life!) involves leaving your reference frame and seeing things from another’s viewpoint. Instead of wondering how far we tilted our heads, <strong>consider how far the other person moved</strong>.</p>

<p><img src="http://betterexplained.com/wp-content/uploads/angles/degrees_vs_radians.png" alt="Degrees vs radians" title="Degrees vs radians"></p>

<p>Degrees measure angles by how far we tilted our heads. Radians measure angles by <strong>distance traveled</strong>.</p>

<p>But absolute distance isn’t that useful, since going 10 miles is a different number of laps depending on the track. So we divide by radius to get a normalized angle:</p>

<p><img src="http://betterexplained.com/latexrender/pictures/9779cb31cc6addc948bc639ade2193a7.gif" title="\displaystyle{Radian = \frac{distance \hspace{10pt} traveled}{radius}}" alt="\displaystyle{Radian = \frac{distance \hspace{10pt} traveled}{radius}}"></p>

<p>You’ll often see this as <img src="http://betterexplained.com/latexrender/pictures/f0974dcee7be289ccb46ac41e9c1f05f.gif" title="\displaystyle{\theta = \frac{s}{r}}" alt="\displaystyle{\theta = \frac{s}{r}}">, or angle in radians = arc length divided by radius. </p>

<p>A circle has 360 degrees or 2pi radians — going all the way around is 2*pi*r / r. So a radian is about 360/2*pi or 57.3 degrees.</p>

<p>Now don’t be like me, memorizing this thinking “Great, another unit. 57.3 degrees is so weird.” Because it is weird when you’re still thinking about you!</p>

<p>Moving 1 radian (unit) is a perfectly normal distance to travel. Put another way, our idea of a “clean, 90 degree angle” means the mover goes a very <em>unclean</em> pi/2 units. Think about it — <em>“Hey Bill, can you run 90 degrees for me? What’s that? Oh, yeah, that’d be pi/2 miles from your point of view.”</em> The strangeness goes both ways.</p>

<p>Radians are the empathetic way to do math — a shift from away from head tilting and towards the mover’s perspective.</p>

<p>Strictly speaking, radians are a ratio (length divided by another length) and don’t have a dimension. Practically speaking, we’re not math robots, and it helps to think of radians as “distance traveled on a unit circle”.</p>

<h2>Using Radians</h2>

<p>I’m still getting used to thinking in radians. But we encounter the concept of “mover’s distance” quite a bit:</p>


<ul>
<li>We use “rotations per minute” not “degrees per second” when measuring certain rotational speeds. This is a shift towards the mover’s reference point (”How many laps has it gone?”) and away from an arbitrary degree measure.</li>
</ul>




<ul>
<li>When a satellite orbits the Earth, we understand its speed in “miles per hour”, not “degrees per hour”. Now divide by the distance to the satellite and you get the orbital speed in radians per hour.</li>
</ul>




<ul>
<li>Sine, that wonderful function, is defined in terms of <em>radians</em> as</li>
</ul>



<p><img src="http://betterexplained.com/latexrender/pictures/f5acdaa25aac3583322e0ba1ed618f44.gif" title="\displaystyle{sin(x) = x - \frac{x^3}{3!} + \frac{x^5}{5!} - \frac{x^7}{7!} ...}" alt="\displaystyle{sin(x) = x - \frac{x^3}{3!} + \frac{x^5}{5!} - \frac{x^7}{7!} ...}"></p>

<p>This formula only works when x is in radians! Why? Well, sine is fundamentally related to <em>distance moved</em>, not head-tilting. But we’ll save that discussion for another day.</p>

<h2>Radian Example 1: Wheels of the bus</h2>

<p>Let’s try a real example: you have a bus with wheels of radius 2 meters (it’s a monster truck bus). I’ll say how fast the wheels are turning and you say how fast the bus is moving. Ready?</p>

<p>“The wheels are turning 2000 degrees per second”. You’d think:</p>


<ul>
<li>Ok, the wheels are going 2000 degrees per second. That means it’s turning 2000/360 or 5 and 5/9ths rotations per second. Circumference = 2 * pi * r, so it’s moving, um, 2 * 3.14 * 5 and 5/9ths… where’s my <a href="http://instacalc.com/?d=&amp;%23038;c=MjAwMCBkZWdyZWVzIHBlciBzZWNvbmR8cm90YXRpb25zID0gMjAwMCAvIDM2MHxyID0gMiBtZXRlcnN8cm90YXRpb25zICogMiAqIHBpICogciAvLyBtZXRlcnMvc2Vjb25kfHx8fHx8&amp;%23038;s=ssssssssss&amp;%23038;v=0.9">calculator</a>…</li>
</ul>



<p>“The wheels are turning 6 radians per second”. You’d think:</p>


<ul>
<li>Radians are distance along a unit circle — we just scale by the real radius to see how far we’ve gone. 6 * 2 = 12 meters per second. Next question.</li>
</ul>



<p>Wow! No crazy formulas, no <a href="http://betterexplained.com/articles/prehistoric-calculus-discovering-pi/">pi</a> floating around — just <em>multiply</em> to convert rotational speed to linear speed. All because radians speak in terms of the mover.</p>

<p>The reverse is easy too. Suppose you’re cruising 90 feet per second on the highway (60 miles per hour) on your 24″ inch rims (radius 1 foot). How fast are the wheels turning?</p>

<p>Well, 90 feet per second / 1 foot radius = 90 radians per second.</p>

<p>That was easy. I suspect rappers sing about 24″ rims for this very reason.</p>

<h2>Radian Example 2: sin(x)</h2>

<p>Time for a beefier example. Calculus is <a href="http://betterexplained.com/articles/a-gentle-introduction-to-learning-calculus/">about many things</a>, and one is what happens when numbers get really big or really small.</p>

<p>Choose a number of degrees (x), and put sin(x) into your calculator:</p>


<p></p>

<p>When you make x small, like .01, sin(x) gets small as well. And the ratio of sin(x)/x seems to be about .017 — what does that mean? Even stranger, what does it mean to multiply or divide by a degree? Can you have square or cubic degrees?</p>

<p>Radians to the rescue! Knowing they refer to distance traveled (they’re not just a ratio!), we can interpret the equation this way:</p>


<ul>
<li>x is how far you traveled along a circle</li>
<li>sin(x) is how high on the circle you are</li>
</ul>



<p>So sin(x)/x is the ratio of how high you are to how far you’ve gone: the amount of energy that went in an “upward” direction. If you move vertically, that ratio is 100%. If you move horizontally, that ratio is 0%.</p>

<p><img src="http://betterexplained.com/wp-content/uploads/angles/sinx_vs_x.png" alt="sin x vs x" title="sin x vs x"></p>

<p>When something moves a tiny amount, such as 0 to 1 degree from our perspective, it’s basically going straight up. If you go an even smaller amount, from 0 to .00001 degrees, it’s <em>really</em> going straight up. The distance traveled (x) is very close to the height (sin(x)).</p>

<p>As x shrinks, the ratio gets closer to 100% — more motion is straight up. Radians help us see, intuitively, why sin(x)/x approaches 1 as x gets tiny. We’re just nudging along a tiny amount in a vertical direction. By the way, this also explains why sin(x) ~ x for small numbers.</p>

<p>Sure, you can rigorously <em>prove</em> this using calculus, but the radian intuition helps you <em>understand</em> it.</p>

<p>Remember, these relationships only work when measuring angles with radians. With degrees, you’re comparing your height on a circle (sin(x)) with how far some observer tilted their head (x degrees), and it gets ugly fast.</p>

<h2>So what’s the point?</h2>

<p>Degrees have their place: in our own lives, we’re the focal point and want to see how things affect us. How much do I tilt my telescope, spin my snowboard, or turn my steering wheel?</p>

<p>With natural laws, we’re an observer describing the motion of others. Radians are about them, not us. It took me many years to realize that:</p>


<ul>
<li>Degrees are <em>arbitrary</em> because they’re based on the sun (365 days ~ 360 degrees), but they are <em>backwards</em> because they are from the observer’s perspective.</li>
<li>Because radians are in terms of the mover, equations “click into place”. Converting rotational to linear speed is easy, and ideas like sin(x)/x make sense.</li>
</ul>



<p>Even angles can be seen from more than one viewpoint, and understanding radians makes math and physics equations more intuitive. Happy math. </p><img src="http://feeds.feedburner.com/~r/Betterexplained/~4/330839580" height="1" width="1"></div>
</div>

<div class="feed">
  <div class="title">66. <a href="http://feeds.feedburner.com/~r/Betterexplained/~3/371265604/" target="_blank">Learning Calculus: Overcoming Our Artificial Need for Precision</a></div>
  <div class="content"><p> Accepting that numbers can do strange, new things is one of the toughest parts of math:</p>


<ul>
<li>There’s numbers <em>between</em> the numbers we count with? (<i>Yes — decimals</i>)</li>
<li>There’s a number for nothing at all? (<i>Sure — zero</i>)</li>
<li>The number line is two dimensional? (<i>You bet — <a href="http://betterexplained.com/articles/a-visual-intuitive-guide-to-imaginary-numbers/">imaginary numbers</a></i>)</li>
</ul>



<p>Calculus is a beautiful subject, but challenges some long-held assumptions:</p>


<ul>
<li>Numbers don’t have to be perfectly accurate?</li>
<li>Numbers aren’t all the same size (i.e. 1 times some number)?</li>
</ul>



<p>Today’s post introduces a new way to think about accuracy and infinitely small numbers. This is not a rigorous course on analysis — it’s my way of grappling with the <a href="http://betterexplained.com/articles/a-gentle-introduction-to-learning-calculus/">ideas behind Calculus</a>.</p>

<h2>Counting Numbers vs. Measurement Numbers</h2>

<p>Not every number is the same. We don’t often consider the difference between the “counting numbers” (1, 2, 3…) and the “measuring numbers” like 2.58, pi, sqrt(2).</p>

<p>Our first math problems involve counting: we have 5 apples and remove 3, or buy 3 books at $10 each. These numbers change in increments of 1, and everything is nice and simple.</p>

<p>We later learn about fractions and decimals, and things get weird:</p>


<ul>
<li>What’s the smallest fraction? (1/10? 1/100? 1/1000?)</li>
<li>What’s the <em>next</em> decimal after 1.0? 1.1? 1.001?</li>
</ul>



<p>It gets worse. Numbers like sqrt(2) and <img src="http://betterexplained.com/latexrender/pictures/7151cab809c0aeeb28ec21bfb56a732e.gif" title="\displaystyle{\pi}" alt="\displaystyle{\pi}"> go on forever, without a pattern. Numbers “in the real world” have all sorts of complexity not found in our nice, chunky counting numbers.</p>

<p>We’re hit with a realization: <strong>we have limited accuracy for quantities that are measured, not counted</strong>.</p>

<p>What do I mean? Find the circumference of a circle of radius 3. Oh, that’s easy; plug r=3 into circumference = 2 * pi * r and get 6*pi. Tada!</p>

<p>That’s cute, but you didn’t answer my question — what <em>number</em> is it?</p>

<p>You may pout, open your calculator and say it’s “18.8495…”. But that doesn’t answer my question either: What, exactly, is the circumference?</p>

<p>We don’t know! Pi continues forever and though we know a trillion digits, there’s infinitely more. Even if we knew what pi was, where would we write it down? We really don’t know the <em>exact</em> circumference of anything!</p>

<p>But hush hush — we’ve hidden this uncertainty behind a symbol, <img src="http://betterexplained.com/latexrender/pictures/7151cab809c0aeeb28ec21bfb56a732e.gif" title="\displaystyle{\pi}" alt="\displaystyle{\pi}">. When you see <img src="http://betterexplained.com/latexrender/pictures/7151cab809c0aeeb28ec21bfb56a732e.gif" title="\displaystyle{\pi}" alt="\displaystyle{\pi}"> in an equation it means “Hey buddy, you know that number, the one related to circles? When it’s time to make a calculation, just use the closest approximation that works for you.” </p>

<p>Again, that’s what the symbol means — we don’t know the real number, so use your best guess. By the way, e and <img src="http://betterexplained.com/latexrender/pictures/023cb7b94f93f82de2a4b0e93e02639c.gif" title="\displaystyle{\sqrt{2}}" alt="\displaystyle{\sqrt{2}}">) have the same caveat.</p>

<h2>40 digits of pi should be enough for anyone</h2>

<p>We think uncertainty is chaos: how can you build a machine unless you know the exact sizes of its parts?</p>

<p>But as it turns out, the “closest approximation of pi that works for us” tends to be surprisingly small. Yes, we’ve computed pi to billions of digits but we only need about 40 for any practical application. </p>

<p>Why? Consider this:</p>


<ul>
<li>Atoms are about <a href="http://trshare.triumf.ca/~safety/EHS/rpt/rpt_1/node7.html">1e-11 meters</a> across</li>
<li>The universe is about <a href="http://en.wikipedia.org/wiki/Universe">90 billion light years</a> (1e27 meters) wide</li>
</ul>



<p>Dividing it out, it takes about 1e38 (1e27 / 1e-11) atoms to span the universe. So, around 40 digits of pi would be enough for an exact count of atoms needed to surround the universe. Were you planning on building something larger than the universe and precise to an atomic level? (If so, where would you put it?)</p>

<p>And that’s just 40 digits of precision; 80 digits covers us in case there’s a mini-universe inside each of our atoms, and 120 digits in case there’s <em>another</em> mini-universe inside of that one.</p>

<p>The point is our instruments have limited precision, and there’s a point where extra detail just doesn’t matter. Pi could become a sudoku puzzle after the 1000th digit and our machines would work just fine.</p>

<h2>But I need exact numbers!</h2>

<p>Accepting uncertainty is hard: what is math if not <em>accurate</em> and <em>precise</em>? I thought the same, but started noticing how often we’re tricked in the real world:</p>


<ul>
<li>Our brains are fooled into thinking 24 images per second is the same as fluid motion.</li>
</ul>




<ul>
<li>Every digital photo (and printed ones, too!) are made from tiny pixels. Pictures seem smooth image until you zoom in:</li>
</ul>



<p><img src="http://betterexplained.com/wp-content/uploads/precision/pixelation.png" alt="pixelation" title="pixelation"></p>

<p>The big secret is that <strong>every digital photo is pixelated</strong>: we only <em>call</em> it pixelated when we happen to notice the pixels. Otherwise, when the squares are tiny enough we’re fooled into thinking we have a smooth picture. But it’s just smooth for human eyes.</p>

<p>This happens to mechanical devices also. At the atomic level, there <a href="http://en.wikipedia.org/wiki/Uncertainty_principle">limits on measurement certainty</a> that restrict how well we can know a particle’s speed and location. Some modern theories suggest a <em>quantized universe</em> — we might be living on a grid!</p>

<p>Here’s the point: approximations are a part of Nature, yet everything works out. Why? <strong>We only need to be accurate within our scale</strong>. Uncertainty at the atomic level doesn’t matter when you’re dealing with human-sized objects.</p>

<h2>Every number has a scale</h2>

<p>The twist is realizing that even <em>numbers</em> have a scale. Just like humans can’t directly observe atoms, some numbers can’t directly interact with “infinitesimals” or infinitely small numbers (in the line of 1/2, 1/3… 1/infinity).</p>

<p>But infinitesimals and atoms aren’t zero. Put a single atom and on your bathroom scale, and the scale still reads nothing. Infinitesimals behave the same way: in our world of large numbers, 1 + infinitesimal looks just like 1 to us.</p>

<p>Now here’s the tricky part: <strong>A billion, trillion, quadrillion, kajillion infinitesimals is still undetectable!</strong> Yes, I know, in the real world if we keep piling atoms onto our scale, eventually it will register as some weight. But not so with infinitesimals. They’re on a different plane entirely — any <em>finite</em> amount of them will simply not be detectable. And last time I checked, we humans can only do things in finite amounts.</p>

<p>Let’s think about infinity for a minute, intuitively:</p>


<ul>
<li>Infinity “exists” but is not reachable by our standard math. No amount of addition or multiplication will take you there — we need an infinite amount of addition to make infinity (circular, right?). Similarly, no finite amount of division will create an infinitesimal.</li>
<li>Infinity and infinitesimals require new rules of arithmetic, just like fractions and complex numbers changed the way we do math. We’ll get into this more later.</li>
</ul>



<p>It’s strange to think about numbers that appear to be zero at our scale, but aren’t. There’s a difference between “true” zero and a measured zero. I don’t fully grasp infinitesimals, but I’m willing to explore them since they make Calculus easier to understand.</p>

<p>Just remember that negative numbers were considered “absurd” even in the 1700s, but imagine doing algebra without them.</p>

<h2>Life Lessons</h2>

<p>Math can often apply to the real world. In this case, it’s the realization that accuracy exists on different levels, and perfect accuracy isn’t needed. We only need 40 digits of pi for our engineering calculations!</p>

<p>When doing market research, would knowing 80% vs 83.45% really change your business decision? The former is 100x less precise and probably 10x easier to get, yet contains almost the same decision-making information.</p>

<p>In science, there’s an idea of <a href="http://en.wikipedia.org/wiki/Significant_figures">significant figures</a>, which help portray uncertainty in our measurements. We’re so used to contrived math problems (”Suzy is driving at 50mph for 3 hours”) that we forget the real world isn’t that clean. Information can be useful even if it’s not perfectly precise.</p>

<h2>Math Lessons</h2>

<p>Calculus was first developed using infinitesimals, which were abandoned for techniques with more “rigor”. Only in the 1960’s (not that long ago!) were the original methods shown to be justifiable, but it was too late — many calculus explanations are separate from the original insights.</p>

<p>Again, my goal is to understand the ideas behind Calculus, not simply rework the mechanics of its proofs. The first brain-bending ideas are that <strong>perfect accuracy isn’t necessary</strong> and that <strong>numbers can exist on different scales</strong>. </p>

<p>There’s a new type of number out there: the infinitesimal. In future posts we’ll see how to use them. Happy math. </p><img src="http://feeds.feedburner.com/~r/Betterexplained/~4/371265604" height="1" width="1"></div>
</div>

<div class="feed">
  <div class="title">67. <a href="http://feeds.feedburner.com/~r/Betterexplained/~3/417347652/" target="_blank">Share your ‘Aha’s: links.betterexplained.com</a></div>
  <div class="content"><p> BetterExplained is about 2 years old, and what ride it’s been. The best part about having a geek diary is interacting with other learning aficionados. My “to read” list grows longer by the week (not that I mind!) and I’ve received some fantastic recommendations.</p>

<p>But that’s all me, me, me.</p>

<p>My friends at <a href="http://slinkset.com/">slinkset</a> let you make a custom news site to share, vote and comment on articles. I’ve always enjoyed the per-article discussions in the comments here, but they’re constrained to specific topics — why not share any link or topic?</p>

<p>Enter <a href="http://links.betterexplained.com">links.betterexplained.com</a>. It’s a repository of links and explanations that gave you that “aha” moment. What can you do?</p>


<ul>
<li><strong>Read good explanations</strong>. When I’m learning a new topic I’ll privately save <a href="http://delicious.com/jshell.com">delicious bookmarks</a>. Now, we can share articles that make us go “aha!” with everyone.</li>
<li><strong>Learn how to communicate well</strong>. When I find an insightful post, I think “That was awesome. How can my articles be like that?”. Usually it’s a combination of style, empathy with the reader, live examples, and useful analogies that make a subject click. Seeing a job well done is a wonderful learning tool. (<i>If you want to learn how to write, read.</i>).</li>
<li><strong>Share your explanations</strong>. Several readers have asked to contribute articles to the site. Right now, I’m the editing bottleneck and can barely keep up with my own posts — avoid this slowpoke and post your story! Any reader can enjoy your epiphanies and give you feedback.</li>
</ul>



<p>I’ve put up some of my <a href="http://links.betterexplained.com/">favorite articles</a> and hope you will do the same (you can contribute anonymously). Happy math, science, business, programming… <img src="http://betterexplained.com/wp-includes/images/smilies/icon_smile.gif" alt=":)">  </p><img src="http://feeds.feedburner.com/~r/Betterexplained/~4/417347652" height="1" width="1"></div>
</div>

<div class="feed">
  <div class="title">68. <a href="http://feeds.feedburner.com/~r/Betterexplained/~3/455802270/" target="_blank">Understanding Debt, Risk and Leverage</a></div>
  <div class="content"><p> I don’t understand all the <a href="http://baselinescenario.com/2008/11/10/baseline-scenario-111008/">dominoes</a> in the financial crisis. In situations like this, it’s helpful to step away and look at general principles: never mind the <a href="http://blog.mint.com/blog/finance-core/a-visual-guide-to-the-financial-crisis/">pieces</a>, what’s the “gravity” that makes them fall? And fall so hard?</p>

<p>Leverage. Leverage is debt used for investment purposes, and is extremely important. Why?</p>


<ul>
<li><b>Debt, when invested, multiplies return (profits and losses)</b></li>
</ul>



<p>Leverage is a multiplier, a super-power. Super-strength is great when times are good, and horrific when you accidentally “bite your tongue” (it’s super-strength, not invulnerability). Concepts like leverage are casually mentioned, but let’s see <strong>why</strong> the dominoes fall.</p>

<h2>Get Rich Quick</h2>

<p>I’ve got a great investment plan for you. Ready?</p>


<ul>
<li>Step 1: Withdraw all your money</li>
<li>Step 2: Go to Las Vegas</li>
<li>Step 3: Bet it all on red in roulette (Get it right and double your money — get it wrong and lose it all)</li>
</ul>



<p>It’s perfect! We’ll double our money in one step.</p>

<p>Sure, there’s a “chance” that things go wrong. But even then it’s no so bad — we’ll be at zero, like the day we were born. Presumably naked and crying as well.</p>

<h2>Double My Money? But I Want More!</h2>

<p>The plan sounds interesting, but there’s a problem — what if I only have $100? Doubling to $200 is nice, but not life-changing.</p>

<p>A few wild thoughts later, and we’re onto a better idea: <strong>let’s borrow money!</strong> The plan becomes exciting:</p>


<ul>
<li>Take our $100 and borrow $1,000,000 from friends, families, banks, and unsavory characters. (How? Well, show different people our $100 and ask to borrow another $100, with our original cash as collateral). </li>
<li>Go to Vegas </li>
<li>Bet the $1M dollars on black! I mean, red!</li>
</ul>



<p>What happens now? </p>

<p></p>

<p>If we win: we get $2M, pay back our $1M loan, and are sitting pretty with our profit of $1M.</p>

<p>And if we lose? <strong>Uh oh.</strong> Now we’re worse than naked: we’ve lost our shirts and everyone else’s too! Because we took debt, our worst case scenario is no longer going broke — it’s going <strong>negative.</strong></p>

<p>Notice how the loan changed the outcomes — neither wild riches nor debtor’s prison were possible without the loan.</p>

<h2>The Risk Multiplier</h2>

<p>What just happened? Debt multiplies our risk and reward. The good times get great, and the bad times become awful. In our example, we went from winning or losing $100 to winning or losing $1M — a 10,000x difference in profit and loss!</p>

<p>This effect from investing debt is called “leverage”. Why?</p>

<p>I <em>suppose</em> it’s because a lever lets you move one end a tiny bit (one inch) and have the other side move a large amount (1 foot). It’s also called a leverage or gearing ratio — move the big gear one cycle and move the small gear many cycles.</p>

<p>My inner geek cringes, since the sides of a lever move in opposite directions (one side up, one side down) and same with the gears (one side clockwise, the other counter-clockwise). Remember that with financial leverage, both <strong>sides move the same way.</strong><br>
I imagine leverage as a game of follow-the-leader: I push my money in one direction (making a bet), and the huge pile of money I borrowed does the same.</p>

<p>Use whatever analogy works for you — the key is if your money wiggles up or down, the borrowed money does the same.</p>

<p><img src="http://betterexplained.com/wp-content/uploads/leverage/leverage.png"></p>

<h2>The Risk and Benefit of Leverage</h2>

<p>Why does leverage work? At its heart, <strong>you are borrowing someone’s assets and reaping the benefits</strong>. It’s like borrowing a cow and selling the milk! What a great idea!</p>

<p>It’s great until the cow runs off. Now you’re stuck — you owe a cow and don’t have one to return. The risk of leverage is investing that debt and losing what you borrowed, which can wipe out any profits.</p>

<p>Let’s try a more realistic example then roulette: investing in a house. Suppose you have 10k and borrow 90k, to purchase a $100k house. You have a leverage ratio of 10:1 — for every 10 dollars of the asset, you’ve put in 1 dollar of equity (your own money).</p>

<p>If house prices rise by 10%, how much did you make? At first blush we’d say 10%, which is true — but you made 10% on the entire 100k! The house is now worth 110k, and after paying your 90k debt you’re left with 20k. That 10% growth became 100% profit on your initial investment!</p>


<ul>
<li><b>leverage ratio = asset / equity</b></li>
<li><b>return = leverage ratio * percent change</b></li>
</ul>



<p>Again, with 10x leverage, 10% growth becomes 100% return on our initial equity. From our analogy, we were in “control” of 10 dollars for every 1 we put in. So, we gained 10x the profit.</p>

<p>Now what about the reverse — when the house falls 10% to 90k?</p>

<p>Well, we can sell the house for 90k, pay off our loan (90k) and are left with… zero! Similarly, a 10% dip in prices becomes a 100% loss of equity — we’re wiped out! We get 10x the loss when prices go south.</p>

<p>And if the house price falls 20% (impossible! improbable! unlikely!), we suffer a 200% loss — we’ve lost our initial 10k and owe 10k beyond that (sell the house for 80k, but the loan is still 90k).</p>

<p>Hopefully the magnifying effect of borrowed money is becoming clear. You lose your equity when the investment drops 1/leverage ratio — in this case, 1/10 or 10%. With a 25x leverage ratio, the investment only needs to drop 4% in order to be wiped out. One way to think about it: you’re paying for losses out of your own pocket, not the borrowed money (you always have to pay it back). Your pocket is only 1 dollar of the 10, so once you lose it (1 dollar out of 10, or 10%) you are wiped out. Any more, and you’re in debt.</p>

<h2>Real-world Examples</h2>

<p>Leverage appears all over finance, but sometimes in disguise. Let’s take a look:</p>

<p><strong>Getting a mortgage</strong>: As we saw, borrowing money to buy a house is a form of leverage. With 5% down (a 20x gearing ratio), your house only needs to drop by 5% to lose money. With 0% down, your house has to drop… wait for it… any amount for you to lose! And after your house is worth less than your mortgage, there’s little incentive to pay it off (better to go bankrupt, depending on the debt).</p>

<p><strong>Lending money for a mortgage</strong>: Even banks are levered. When they offer money, where do you think they get it? From deposits! They borrow your deposits to loan it out to other people. If they have 10k of deposits they might loan out 100k (there’s some magic that happens here, how banks loan more money than they have; that’s for another time). If they loaned money for a house, and the house drops 10% in value and the debtor doesn’t pay, the bank has lost all if its deposits.</p>

<p><strong>Offering mortgage insurance</strong>: Insurance companies might have 10k worth of cash, and offer 100k worth of insurance coverage to banks (C’mon, did you really think the insurance company had enough to pay off everyone’s claims?). Of course they don’t expect <em>everyone</em> to file a claim, but if even 10% of people do, they are wiped out. There isn’t an explicit loan, but the insurance company has created an obligation to pay (called the insurance leverage ratio).</p>

<p>See the set up? When prices are rising:</p>


<ul>
<li>Owners are making a lot of money (they leveraged the house)</li>
<li>Banks are making a lot of money (they leveraged their loans, and earn a lot of interest on the borrowed money)</li>
<li>Insurance companies are making a lot of money (they’re offer more coverage, which means more premiums)</li>
</ul>



<p>If the music stops and house prices fall, problems arise:</p>


<ul>
<li>Borrowers lose equity — a 5% drop when 20x levered means the borrower is wiped out. Any more and the loan is worth more than the house.</li>
<li>Banks lose loans — if 5% of loans go bad, or if they can only recover 95% of the house’s value, the bank lost all the money it put in!</li>
<li>Insurance companies lose money — if 5% of claims are called in, when the insurance company is 20x levered, it means the company has lost all of its assets!</li>
</ul>



<p>For 20x leverage, a 5% drop would wipe you out to zero equity. Any more and you’re going negative — you’re at zero equity and still owe money!</p>

<h2>The Lessons</h2>



<p>I don’t understand the crisis, but I’m getting a grasp on leverage: it’s the gravity that pulls down the dominoes. In fact, it can multiply the dominoes as they fall! Here are the key points:</p>


<ul>
<li><strong>Leverage multiplies profits and losses:</strong> You can make a “regular” investment swing as wildly as you like by borrowing money.</li>
<li><strong>Return = leverage ratio * percent change</strong>: A meager 10x gearing ratio can double your money with a 10% gain, or wipe you out with a 10% loss. By the end of a crisis, some banks increased their <a href="http://en.wikipedia.org/wiki/Image:Leverage_Ratios.png">leverage ratio to 30:1</a> — if prices fell even 3% they would be wiped out!</li>
<li><strong>Leverage appears everywhere</strong>: Companies have debt/equity ratios (how levered they are) and stock portfolios have beta (riskiness beyond the market average, which is increased by debt). Whenever you see debt or investment, look to see if it’s leveraged in some way.</li>
</ul>



<p>Leverage make the boom times better and the busts harsher. The financial crisis has many other effects in play (such as the liquidity crisis, which makes it difficult to sell the assets you have to pay off your debts), but let’s take one idea at a time. A <a href="http://blog.craigdossantos.com/2008/11/02/how-anyone-can-understand-the-financial-crisis/">good friend</a> found <a href="http://www.thislife.org/Radio_Episode.aspx?episode=355">some</a> <a href="http://www.thisamericanlife.org/Radio_Episode.aspx?episode=365">podcasts</a> on the crisis — if you’ve found a resource that helps you get the crisis, feel free to share it below. </p><img src="http://feeds.feedburner.com/~r/Betterexplained/~4/455802270" height="1" width="1"></div>
</div>

</body></html>